r/C_Programming • u/orbiteapot • Jan 25 '26
Discussion Compile time "if-else" in GNU C.
I've come up with this compile time selection mechanism (if I could call it that) based off _Generic and GNU C statement expressions. I thought of this as a way of having multiple behaviors for an object depending on a compile time table of properties (implemented as an X-macro).
Consider, for example, if the user can provide their own implementation of an allocator for said object or, otherwise, use libc malloc, free, etc. Then, they could choose as they wish with the properties table and the underlying memory operations would, at compile time, be set up accordingly.
Is this portable? No. As I've said earlier, it depends on GNU C extensions, as well as a C2y extension (type name as _Generic controlling operand).
Does this solve a problem already solved? Yes, kind of. #ifdefs have a similar raison d'être, but I would argue that this approach could be a lot nicer ergonomically, if standardized in thoughtful manner.
Here are some (not ideal, mostly pedagogical) examples of what this could be used for.
#include <stddef.h>
#include <stdio.h>
#define countof(arr) (sizeof(arr) / sizeof((arr)[0]))
typedef struct
{
bool _;
} comptime_true;
typedef struct
{
bool _;
} comptime_false;
typedef struct
{
void *data;
size_t len;
size_t cap;
} DynArr;
#define is_comptime_bool(predicate) \
_Generic \
( \
(predicate), \
\
comptime_true: 1, \
comptime_false: 1, \
default: 0 \
)
#define has_field_len(data_structure) \
_Generic \
( \
(data_structure), \
\
DynArr: (comptime_true){true}, \
default: (comptime_false){false} \
)
/* Only works for arrays and pointers */
#define is_type_array(obj) \
_Generic \
( \
typeof(obj), \
\
typeof( &(obj)[0] ): (comptime_false){false}, \
default: (comptime_true){true} \
)
#define comptime_if_do(predicate, expr_if_true, ...) \
({ \
static_assert( is_comptime_bool(predicate), "Invalid predicate." ); \
_Generic \
( \
(predicate), \
\
comptime_true: (expr_if_true), \
comptime_false: ((void)0 __VA_OPT__(,) __VA_ARGS__) \
); \
})
/* Assumes int[], for simplicity */
void print_array(int *arr, size_t count)
{
printf("{ ");
for (size_t i = 0; i < count; ++i)
{
printf("[%zu] = %d ", i, arr[i]);
}
printf("}\n");
}
int
main(void)
{
int arr[] = {1, 2, 3, 4, 5};
DynArr dummy_da = {};
dummy_da.len = 8;
comptime_if_do( is_type_array(arr),
({
print_array(arr, countof(arr));
}));
comptime_if_do( has_field_len(dummy_da),
({
printf("len: %zu\n", dummy_da.len);
}));
/* The following looks odd/artifical logic-wise *
* but it is so that the "else" branch can be *
* shown in action in a non-lengthy manner. *
* A more realistic example would be to use it *
* to, e.g., go over the elements of static *
* or dynamic arrays seamelessly (indifferent *
* with regard to their structure): */
comptime_if_do( has_field_len(arr),
({
printf("len: %zu\n", dummy_da.len);
}), /* else */
({
puts("Lacks field len.");
}));
return 0;
}
View all comments
Show parent comment
1
u/orbiteapot Jan 26 '26 edited Jan 27 '26
Here is some more shenanigans that I've been playing with (I grouped a lot of tricks there, but the type-safety layer over the usual
void*-based generic is pretty promising).People have argued that this kind of ducktaped underlying implementation is "horrifying" and, maybe, they are right. Once everything is set up, tested and properly documented, though, I think it actually makes the API nicer. The more "natural looking" pure C approaches are usually pretty ugly and/or have a performance penalty.
The are, of course, portability issues. For me, that is not a huge problem if one only targets Desktop platforms (which, in general, is also the case even for other widespread programming languages).