public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ Macros, ##, and ::
@ 2008-03-27 21:00 Harvey Chapman
  2008-03-27 22:39 ` Daniel Lohmann
  2008-03-31 23:38 ` Philipp Thomas
  0 siblings, 2 replies; 4+ messages in thread
From: Harvey Chapman @ 2008-03-27 21:00 UTC (permalink / raw)
  To: MSX to GCC

[-- Attachment #1: Type: text/plain, Size: 737 bytes --]

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

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 194 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C++ Macros, ##, and ::
  2008-03-27 21:00 C++ Macros, ##, and :: Harvey Chapman
@ 2008-03-27 22:39 ` Daniel Lohmann
  2008-03-30 19:28   ` Nathan Sidwell
  2008-03-31 23:38 ` Philipp Thomas
  1 sibling, 1 reply; 4+ messages in thread
From: Daniel Lohmann @ 2008-03-27 22:39 UTC (permalink / raw)
  To: Harvey Chapman; +Cc: MSX to GCC


On 27.03.2008, at 21:59, Harvey Chapman wrote:
> 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.

AFAIK :: is a delimiter to the preprocessor (similar to ,).  Hence,  
the first one should be portable; the latter one does not work as  the  
result of the concatenation has to be a token.

To clarify this in the code , you could even write:

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


(Well, just a guess -- I am *never* sure when it comes to the  
preprocessor....)


Daniel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C++ Macros, ##, and ::
  2008-03-27 22:39 ` Daniel Lohmann
@ 2008-03-30 19:28   ` Nathan Sidwell
  0 siblings, 0 replies; 4+ messages in thread
From: Nathan Sidwell @ 2008-03-30 19:28 UTC (permalink / raw)
  To: Daniel Lohmann; +Cc: Harvey Chapman, MSX to GCC

Daniel Lohmann wrote:
> 
> On 27.03.2008, at 21:59, Harvey Chapman wrote:
>> Why does ...
>>
>> #define MT(name) { name::Type, name::Create }
>>
>> work, and ...
>>
>> #define MT(name) { name##::Type, name##::Create }
>>
>> does not?

> AFAIK :: is a delimiter to the preprocessor (similar to ,).  Hence, the 
> first one should be portable; the latter one does not work as  the 
> result of the concatenation has to be a token.
> 
> To clarify this in the code , you could even write:
> 
>> #define MT(name) { name :: Type, name :: Create }

you are correct on all points. '::' is a token in its own right.

nathan.

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::         CodeSourcery

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C++ Macros, ##, and ::
  2008-03-27 21:00 C++ Macros, ##, and :: Harvey Chapman
  2008-03-27 22:39 ` Daniel Lohmann
@ 2008-03-31 23:38 ` Philipp Thomas
  1 sibling, 0 replies; 4+ messages in thread
From: Philipp Thomas @ 2008-03-31 23:38 UTC (permalink / raw)
  To: gcc-help

On Thu, 27 Mar 2008 16:59:51 -0400, Harvey Chapman wrote:

>#define MT(name) { name::Type, name::Create }
>
>work, and ...
>
>#define MT(name) { name##::Type, name##::Create }
>
>does not?

Because name, '::' and Type are each distinct tokens, i.e. you don't
need to concat them. The standard says that the result of token pasting
again needs to be a valid preprocessing token and name::Type isn't.

Philipp
-- 
This is the Gentoo generation: Can't program but wants to compile
everything from source.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-03-31 23:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-27 21:00 C++ Macros, ##, and :: Harvey Chapman
2008-03-27 22:39 ` Daniel Lohmann
2008-03-30 19:28   ` Nathan Sidwell
2008-03-31 23:38 ` Philipp Thomas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).