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

Joel C. Salomon joelcsalomon at gmail.com
Thu Jul 14 13:01:36 PDT 2011


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


More information about the Cfp-interest mailing list