C11 has a _Generic macro that can allow the use of cool common functions. However, using true and false with this leads to incorrect deduction in the normal case:
#include <stdio.h> #include <stdbool.h> #define TypeName(x) \ _Generic((x), \ bool: "bool", \ int: "int", \ default: "unknown") #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && (__bool_true_false_are_defined) # undef true # define true ((bool)(1)) # undef false # define false ((bool)(0)) #endif int main(void) { printf("1: %s\n", TypeName(1)); printf("true: %s\n", TypeName(true)); printf("false: %s\n", TypeName(false)); }
Fingerprints:
1: int true: bool false: bool
However, without the middle bit, which overrides true and false :
1: int true: int false: int
This means that you cannot execute _Generic functions, such as:
struct Variant * const int32 = variant_create(1); struct Variant * const boolean = variant_create(true);
So my questions are :
- Is redefinition fragment safe?
- Is it C11 oversight or a bug in GCC and Clang?
source share