[Cfp-interest] C1x's _Generic and totalorder()
Joel C. Salomon
joelcsalomon at gmail.com
Fri Jul 15 09:14:28 PDT 2011
On 07/14/2011 05:08 PM, Stephen Canon amended my suggestion thus:
> #define totalorder(x,y) \
> _Generic((x), float: _Generic((y), \
> float: totalorderf, \
> double: _Static_assert(0, "type mismatch")), \
> double: _Generic((y), \
> float: _Static_assert(0, "type mismatch")), \
> double: totalorder))((x),(y))
>
> but it still fails for exactly the reason that Joel highlighted;
> _Generics expand to expressions, which _Static_assert is not.
On comp.lang.c, Jens Gustedt suggested using an invalid constant
expression, e.g., sizeof (double[-1]), in place of the _Static_assert.
Ben Bacarisse was unsure whether that would work (see the thread "Static
Assertions that are Expressions",
<http://groups.google.com/group/comp.lang.c/browse_thread/thread/0e0f381ab1060113>,
for details), and suggested instead:
> Why not just remove the option of the "bad" types in the inner
> _Generic expressions? A type for y that is not compatible with the
> only listed option is a constraint violation so a diagnostic is
> required.
I don't have a compiler that implements _Generic, so I can't test this:
#define totalorder(x,y) _Generic((x), \
float: _Generic((y), float: totalorderf), \
double: _Generic((y), double: totalorder)) ((x),(y))
I'm particularly unsure whether this will catch the incorrect usage
totalorder(1.0, 2.0f)
or whether the float argument will be considered "compatible" with the
double generic-association.
--Joel Salomon
More information about the Cfp-interest
mailing list