[Cfp-interest] C1x's _Generic and totalorder()

Stephen Canon scanon at apple.com
Fri Jul 15 10:11:13 PDT 2011


Using ToT clang (which is available to you Joel, if you want to play with this stuff, so long as you don't mind building your tools), I get the following error with the amended suggestion:

clang foo.c -c
foo.c:10:12: error: controlling expression type 'float' not compatible with any generic
      association type
    return totalorder(1.0f, 1.0f);
           ^~~~~~~~~~~~~~~~~~~~~~
foo.c:10:29: note: instantiated from:
    return totalorder(1.0f, 1.0f);
                            ^~~~
1 error generated.

I'm not sure that this error is actually warranted, however (I certainly can't make sense of it), so I'm going to ping our C1x guru here at Apple and see what he thinks about this.

- Steve

On Jul 15, 2011, at 12:14 PM, Joel C. Salomon wrote:

> 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