[comp.lang.c++] sizeof as a macro

culliton@cdss.UUCP (Tom Culliton) (04/10/90)

In article <58@cdss.UUCP> I wrote....
> Another thought that stems from this and a little puzzle :-) how would you
> implement sizeof as a macro?

And In article <1448@tkou02.enet.dec.com>,
 diamond@tkou02.enet.dec.com (diamond@tkovoa) replied:
> I don't think you can.  How can a macro compute sizeof(-3) without using the
> builtin sizeof?  (And in case you manage that, here are two more for you:
> sizeof -3 (no parentheses needed with an expression), and sizeof(int).)

Which is of course absolutely correct.  Since I don't generally use sizeof
like that it just didn't occur to me.  I think the idea is still intresting
though.  Let me rephrase it to keep myself out of trouble. (a very difficult
business) ;^)

Given:  #define sizeof(type) ...

How would you implement the right hand side?  BTW "type" is just that, a
legitimate type, like "int", "char [10]", "struct XYZ", "[class] WINDOW", etc.

Just to forestall any flames, yes, defining macros over keywords is a VERY BAD
idea, I neither do it nor advocate it, and duplicating built in funtionality
is just as bad.  However, thinking about how such things are or can be done is
mind broadening.
                                                      Tom
*****************************************************************************
*  uunet!culliton@cdss - Tom Culliton at Arinc Research Corp. Annapolis MD  * 
*       "I haven't lost my mind -- it's backed up on tape somewhere."       *
*****************************************************************************