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

Stephen Canon scanon at apple.com
Thu Jul 14 14:08:48 PDT 2011


At least with the clang implementation of _Generic, this fell over with several errors.  I was able to mostly patch it up like so:

#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.  I don't see any straightforward way around this, unfortunately.  One could replace the _Static_assert with a function that causes a runtime failure, but that's not at all satisfactory.

- Steve

On Jul 14, 2011, at 4:01 PM, Joel C. Salomon wrote:

> Folks,
> 
> Here's what I had in mind for using C1x's _Generic to catch invalid uses
> of totalorder(x,y):
> 
> int totalorder(double x, double y);
> int totalorderf(float x, float y);
> 
> #define totalorder(x, y) _Generic((x), \
> 	float: _Generic((y),  \
> 		float: totalorderf((x), (y)) \
> 		)                            \
> 	double: _Generic((y), \
> 		float: _Static_assert(0, "type mismatch"), \
> 		double: totalorder((x), (y))               \
> 		)                                          \
> 	)
> 
> (I'm not actually sure the _Static_assert is completely correct; N1570
> §6.7.10 implies that static assertions need to be statements, not
> expressions, so a line like
> 
> 	int i = totalorder(1.0, 2.0f) + 17;
> 
> may give the wrong compile error.)
> 
> Does this seem a reasonable path to continue down?
> 
> --Joel Salomon
> _______________________________________________
> Cfp-interest mailing list
> Cfp-interest at oakapple.net
> http://mailman.oakapple.net/mailman/listinfo/cfp-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.oakapple.net/pipermail/cfp-interest/attachments/20110714/54c75e7b/attachment.html 


More information about the Cfp-interest mailing list