[comp.std.c] I assert Plauger's header is wrong

hugh@dgp.toronto.edu (D. Hugh Redelmeier) (09/15/90)

In article <1428@proto.COM> joe@proto.COM (Joe Huffman) writes:
>In the latest version of 'The C User Journal' P.J. Plauger wrote about the
>assert 'function' under ANSI C.  He outlined various 'sins' of many 
>implementations of assert and 'blessed' the following version.  It appears
>to me that this is wrong (it would break nearly all of my code).
>
>----
>/* assert.h standard header
> * copyright (c) 1990 by P.J. Plauger
> */
>#undef assert/* remove any previous definition */
>#ifdef NDEBUG
>
>#define assert(test) ((void)0)
>
>#else /* NDEBUG not defined */
>
>void _Assert(char *);
>
>#ifndef _STR /* define stringize macro just once */
>#define _STR(x) #x
>#endif
>
>#define assert(test) ((test) || _Assert(__FILE__ ":" _STR(__LINE__) " " #test))
>#endif

I think that there are (at least) three bugs in PJP's assert.h:

1. || does not accept void on its right-hand side, but PJP puts a
   call to _Assert on the right-hand side of ||, and _Assert is
   defined to return void.

2. assert's return type should be void, yet PJP's assert returns the
   result of ||, an int.

3. In ANSI C, _STR(__LINE__) would yield "__LINE__", not,
   say, "937".  To get what he wants, he needs to use xstr from pg 93
   of the standard.

   Sample:

	#define	str(s)	#s
	#define	test1	str(__LINE__)

	#define	xstr(s)	str(s)
	#define	test2	xstr(__LINE__)

	test1	/* yields "__LINE__" */

	test2	/* yields "9" */

Hugh Redelmeier
{utcsri, yunexus, uunet!attcan, utzoo, scocan}!redvax!hugh
When all else fails: hugh@csri.toronto.edu
+1 416 482-8253