PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > A problem about concatenation in macro
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
A problem about concatenation in macro

Réponse
 
LinkBack Outils de la discussion
Vieux 18/01/2008, 02h58   #1
cppcraze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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 me out.

- Martin
  Réponse avec citation
Vieux 18/01/2008, 03h54   #2
red floyd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A problem about concatenation in macro

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 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 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.

  Réponse avec citation
Vieux 18/01/2008, 07h56   #3
jalina
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A problem about concatenation in macro

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 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 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.
  Réponse avec citation
Vieux 18/01/2008, 13h11   #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
Vieux 18/01/2008, 13h13   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A problem about concatenation in macro

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 ## =
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é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
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h30.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13842 seconds with 13 queries