C++ Macros, ##, and ::

Harvey Chapman hchapman-gcc-help@3gfp.com
Thu Mar 27 21:00:00 GMT 2008


Why does ...

#define MT(name) { name::Type, name::Create }

work, and ...

#define MT(name) { name##::Type, name##::Create }

does not?

Is that first one portable? I understand the error I get for the  
second one about name and :: not constituting a valid pre-processor  
token, but the first one seems wrong to me. Is :: used in a macro a  
special case? Or is it an operator? Just looking for some insight to  
help me remember this.

Here is the original code to give you an idea of intent.

#define MT(name) { name::Type, name::Create }

struct types_t
{
   uint32_t type;
   BaseModel::CreateFn fn;
} types[] =
{
   MT(Model800),
   MT(Model900)
};
uint32_t typesLen = sizeof(types)/sizeof(types_t);

#undef MT


Thanks,

Harvey
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 194 bytes
Desc: This is a digitally signed message part
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20080327/b011a9e9/attachment.sig>


More information about the Gcc-help mailing list