e1?(void_e2):(void_e3)

Chris Torek chris at umcp-cs.UUCP
Thu Aug 14 13:01:01 AEST 1986


In article <243 at desint.UUCP> geoff at desint.UUCP (Geoff Kuenning) writes:
>... let us not forget that there is *no* reason to ever write this
>particular expression.  Anywhere it's useful, you can just write
>
>	if (e1)
>	    void_e2;
>	else
>	    void_e3;

Not so!  The above is a statement, while `e1 ? void_e2 : void_e3' is an
expression; and expressions are useful in macros:

	#define strange()	((e1 ? void_e2 : void_e3), arbitrary_e4)

Using

	#define	strange()	if (e1) void_e2; else void_e3; arbitrary_e4

means that

	if (x) strange(); else ...

no longer works.

As a last resort, I now use the following for inline functions
returning void:

	#ifdef lint
	#define _false_ rand()
	#else
	#define _false_ 0
	#endif

	#define	strange() \
		do { \
			s1; \
			... \
			sn; \
		} while (_false_)

The funny `_false_' keeps lint from warning about constants in
conditional contexts.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list