8
我想用C11_Generic
关键字根据静态类型,以填补工会,如:_Generic填补一些工会
typedef union {
double d;
long l;
const char*s;
void*p;
} ty;
#define make_ty(X) _Generic((X), \
double: (ty){.d=(X)}, \
long: (ty){.l=(X)}, \
const char*: (ty){.s=(X)}, \
default: (ty){.p=(X)})
ty from_double(double x) { return make_ty(x); }
ty from_string(const char*s) { return make_ty(s); }
ty from_long(long l) { return make_ty(l);}
但是这并不编译,例如GCC 5.3给出了(与gcc -std=c11 -Wall
):
u.c: In function ‘from_double’:
u.c:11:35: error: incompatible types when initializing type ‘const char *’
using type ‘double’
const char*: (ty){.s=(X)}, \
^
u.c:14:41: note: in expansion of macro ‘make_ty’
ty from_double(double x) { return make_ty(x); }
BTW,使用gcc -std=c99 -Wall
给出了同样的错误...
还是_Generic
唯一有用tgmath.h
?
我认为_Generic
根据编译器已知类型选择表达,所以无意义的(ty){.s=(x)}
将在from_double
....
被忽略(如果没有工作,我将能够“超载” make_ty
根据静态,编译器知道,类型参数的...)
好吧,但即使在'-std = c11'这不起作用 –
[This question](http://stackoverflow.com/questions/24743520/incompatible-pointer-types-passing-in-generic-macro)有一个类似的问题,最好的答案可能会回答你的问题 –
关于同一主题的其他答案似乎表明,其他案件不允许包含*违反约束*(错误类型的初始化确实是其中之一) –