On 10/19/2009 09:15 PM, Jamie Lokier wrote:
__builtin_types_compatible_p(), __builtin_choose_exper and
__attribute__((__error__)) are good for informative error messages.
Unfortunately __attribute__((__error__)) cannot be applied in this case
for several reasons:
1) it cannot apply to types so you cannot do
extern void compile_error (void);
#define raise_compile_error(x) \
(((void __attribute__ ((error(x))) (*) (void)) x) ())
2) it only operates for calls, so you cannot use it in initializers
I whipped up a patch to add to GCC __builtin_compile_error, to be used
inside __builtin_choose_expr:
#define typecheck(t1, t2) \
__builtin_choose_expr (__builtin_types_compatible(t1, t2), (void)0, \
__builtin_compile_error (#t1 " and " #t2 \
"are not compatible types"))
#define if_types_compatible(t1, t2, value) (typecheck (t1, t2), value)
and I will send it upstream, but it will be a long time before it is in
a released version (especially since GCC is currently in bug-fixing-only
mode)---anyway Jamie got the array case to work, AFAIU.
Paolo