A problem about concatenation in macro



 DEVELOP > c-Plus-Plus > A problem about concatenation in macro

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "cppcraze"
Date: 17 Jan 2008 08:58:48 PM
Object: A problem about concatenation in macro
Hi,
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 help 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 ? Hope someone can help me out.
- Martin
.

User: "James Kanze"

Title: Re: A problem about concatenation in macro 18 Jan 2008 07:11:15 AM
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 help 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=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.

User: "jalina"

Title: Re: A problem about concatenation in macro 18 Jan 2008 01:56:15 AM
cppcraze a écrit :

Hi,

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 help 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 ? Hope someone can help me out.

- Martin

THe ## is used to make a new *single* token from two others. Since
::##arg gives (e.g. for A) ::A which is not a single token.
.

User: "red floyd"

Title: Re: A problem about concatenation in macro 17 Jan 2008 09:54:57 PM
cppcraze wrote:

Hi,

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 help 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 ? Hope someone can help me out.

The token pasting operator ## creates a new identifier. Since in the
X:: sequence, arg is an independent identifier, and not pasted to
anything (like "get"), just use X::arg, and it will expand properly.
.
User: "James Kanze"

Title: Re: A problem about concatenation in macro 18 Jan 2008 07:13:51 AM
On Jan 18, 4:54 am, red floyd <no.s...@here.dude> wrote:
[...]

The token pasting operator ## creates a new identifier.

Not necessarily a new identifier. For example, I've used it for
things like:
op ## =3D
where the legal values of op were +, -, etc.
The only real requirement is that the results form a legal
token.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER