檢查變數是否屬於某種限定型別
#include <stdio.h>
#define is_const_int(x) _Generic((&x), \
const int *: "a const int", \
int *: "a non-const int", \
default: "of other type")
int main(void)
{
const int i = 1;
int j = 1;
double k = 1.0;
printf("i is %s\n", is_const_int(i));
printf("j is %s\n", is_const_int(j));
printf("k is %s\n", is_const_int(k));
}
輸出:
i is a const int
j is a non-const int
k is of other type
但是,如果型別泛型巨集實現如下:
#define is_const_int(x) _Generic((x), \
const int: "a const int", \
int: "a non-const int", \
default: "of other type")
輸出是:
i is a non-const int
j is a non-const int
k is of other type
這是因為為了評估 _Generic
主表示式的控制表示式而丟棄所有型別限定符。