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

Kurt Baudendistel baud at eedsp.eedsp.gatech.edu
Tue Jan 16 09:54:29 AEST 1990


GCC, the GNU C compiler, implements the ``typeof'' operator as it is being
discussed here. It allows an argument that can be an expression (an
r-value) or a type. The ``typeof'' construct can be used anywhere a typedef
name could be used (in any declaration or definition).

Be careful, however, of the nasty C preprocessor, which will make this
nice looking and appealing definition:


  #define SWAP(a,b) {typeof (a) tmp; tmp=a; a=b; b=tmp}
 
			[courtesy of peter at ficc.uu.net (Peter da Silva)]

fail in many cases, like

  if (x < y)
    SWAP (x, y);	// bracketing of SWAP makes the `;' extraneous
  else			// and fatal
    x = y;

or

  int tmp;
  ...
  SWAP (x, tmp);	// name ``tmp'' in SWAP makes this fail

or

  int a[10], b[10];
  ...
  SWAP (a, b);		// will this work? 
			// depends on your definition of `='

can you think of other pitfalls? 
kurt
-- 
Kurt Baudendistel --- GRA
Georgia Tech, School of Electrical Engineering, Atlanta, GA  30332
internet: baud at eedsp.gatech.edu         uucp: gatech!gt-eedsp!baud



More information about the Comp.lang.c mailing list