Typeof operator in C (Re: An Interesting View of "Strong" Vs. "Weak" Typing)

Wm E. Davidsen Jr davidsen at sixhub.UUCP
Tue Jan 16 12:48:51 AEST 1990


In article <5263 at buengc.BU.EDU> bph at buengc.bu.edu (Blair P. Houghton) writes:

| >But a typeof operator... wouldn't that be something...
| >
| >#define SWAP(a,b) {typeof a tmp; tmp=a; a=b; b=tmp}
| 
| Oboy.  Saved a few char's in the source code...

  That's not what it's for. You have avoided a possible error caused by
changing the type of the items to be swapped. By forcing the temp to be
the same type as the items you avoid a type conversion, which could be
(a) slow and (b) might generate a roundoff or data loss error.

  Now my question (no gcc docs on this machine) is, does this return a
numeric value? In other words, can I improve my error checking in the
macro by saying something like:
	#define SWAP(a,b) {typeof a tmp;\
	assert(typeof a == typeof b);\
	tmp = a; a = b; b = tmp; }

  All this requires is that the evaluation return a discrete value for
all types, but the user defined types would be a problem. I can envision
a PRINT macro which would use typeof to select the format of the print.
I can envision it, and it's UGLY...
-- 
	bill davidsen - sysop *IX BBS and Public Access UNIX
davidsen at sixhub.uucp		...!uunet!crdgw1!sixhub!davidsen

"Getting old is bad, but it beats the hell out of the alternative" -anon



More information about the Comp.lang.c mailing list