This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// stdtypetraits.h | |
// | |
// start of a traits header | |
// | |
// a function identifier will always decay to a pointer to itself | |
#define SelfDecays(V) _Generic ((0, V) \ | |
, typeof(V) * : true \ | |
, default : false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define IsArray(V) _Generic((0, V) \ | |
, typeof(V): false \ | |
, default: true) | |
char ca[] = "hello"; | |
int ia[10] = {}; | |
static_assert (IsArray(ca)); | |
static_assert (IsArray(ia)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Simple (i.e. very limited) metaprogramming macros | |
// | |
#ifndef C_MACROS_H | |
#define C_MACROS_H 1 | |
#define M_MAX_DEPTH 32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
// typeclasses | |
#define ToLvalue(V) (&(struct { typeof (V) v; }){ V }.v) | |
typedef void (*BinOp) (void * out, void const * lhs, void const * rhs); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "typeid.h" | |
_Static_assert (TYPEID (void) == 0, ""); | |
_Static_assert (TYPEID (int) == 6, ""); | |
// _Static_assert (TYPEID (int) == 13, ""); // fails | |
_Static_assert (TYPEID (double) == 13, ""); | |
// _Static_assert (TYPEID (double) == 7, ""); // fails |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// counter.h | |
// | |
// increment the value of the counter | |
// every time this file is included | |
// | |
// no guard, repeatable inclusion | |
#ifndef MY_COUNTER // starting value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// basic array mapper | |
// we can use it with mis-typed arguments | |
typedef void (* Mutate) (void *, void *); | |
typedef void * (* Step) (void *); | |
void addOneInt (void * in, void * out) { *(int *)out = *(int *)in + 1; } | |
void addOneFloat (void * in, void * out) { *(float *)out = *(float *)in + 1.0f; } |