Afficher un message
Vieux 18/01/2008, 14h11   #4
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A problem about concatenation in macro

On Jan 18, 3:58 am, cppcraze <cppcr...@gmail.com> wrote:
> I am just stumbled by a problem about concatenation in macro. See
> below code snippet:


> // there're some contants definition in this class
> struct X
> {
> enum {A, B, C};
> };


> // and here I want to define a utility macro to me generate some
> // functions
> #define MK_FUNC(arg) \
> int get##arg() \
> { \
> return X::##arg; \
> }


> MK_FUNC(A)
> MK_FUNC(B)
> MK_FUNC(C)


> #undef MKFUNC


> // then I can use getA(), getB() .... in my program.


> But the preprocessor always complains:


> warning: pasting "::" and "A" does not give a valid preprocessing
> token
> warning: pasting "::" and "B" does not give a valid preprocessing
> token
> warning: pasting "::" and "C" does not give a valid preprocessing
> token


> I really don't why this will happen. Isn't this usage in the macro
> "X::##arg" an invalid?


It's invalid. The string "X::##arg" breaks down into the tokens
X, ::, ## and arg. You're trying to paste :: and arg to get a
single token, and that isn't a valid token in C++.

Why do you paste at all here? Isn't the token you want
precisely the expansion of arg?.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
 
Page generated in 0,05937 seconds with 9 queries