[vox-tech] C preprocessor - "soft reference"?

Mark K. Kim lugod3MAPS at cbreak.org
Fri Jan 19 18:43:29 PST 2007


On Fri, Jan 19, 2007 at 03:23:24PM -0500, Peter Jay Salzman wrote:
[snip]
> > #define CHECK_FOR_NULLITITY(var) \
> > 	if ( (var) == NULL ) \
> > 	{ \
> > 		puts( #var " is null." ); \
> > 		exit(EXIT_FAILURE); \
> > 	}
[snip]
> Yeah, in the real code I did.  I was actually using cerr (it's C++ code).

There's another oddity with macros when using with C++ streams:

  #define PRINT_ME(a)  do { std::cout << a << std::endl; } while(0)

  int main() {
    int i = 1;

    PRINT_ME(i+5);
  }

This will generate error, because PRINT_ME(i+5) expands to:

  std::cout << i+5 << std::end;

and since + has higher precedence than <<, syntax error gets generated
at compile time.  Once again, the "enclose everything in define" rule of
thumb kicks in:

  #define PRINT_ME(a)  do { std::cout << (a) << std::endl; } while(0)

which allows the expansion of the macro to be:

  std::cout << (i+5) << std::end;

and all is well...

-Mark



More information about the vox-tech mailing list