Preprocessor concatenation of operator and ...

M

mrstephengross

I'm using gcc 3.3.1 to compile the following code (below). I've written
a macro to simplify writing operators. The macro uses the '##' operator
to paste together 'operator' and the name of the operator (ie: '+').
gcc reports that pasting the two things together "does not give a valid
preprocessing token". Here's the code:

====================
#define OPER(op) operator ## op

class thing
{
public:

bool OPER(==) (const thing & other) const { return true; }
};


int main() { return 0; }
=============================

The error reads: (line 7): pasting "operator" and "==" does not give a
valid preprocessing token

Any ideas?

Thanks,
--Steve ([email protected])
 
D

Dan Cernat

mrstephengross said:
I'm using gcc 3.3.1 to compile the following code (below). I've written
a macro to simplify writing operators. The macro uses the '##' operator
to paste together 'operator' and the name of the operator (ie: '+').
gcc reports that pasting the two things together "does not give a valid
preprocessing token". Here's the code:

====================
#define OPER(op) operator ## op

class thing
{
public:

bool OPER(==) (const thing & other) const { return true; }
};


int main() { return 0; }
=============================

The error reads: (line 7): pasting "operator" and "==" does not give a
valid preprocessing token

Any ideas?

Thanks,
--Steve ([email protected])

Compiles fine with VC 6.0 and 7.1

/dan
 
M

Maett

mrstephengross said:
I'm using gcc 3.3.1 to compile the following code (below). I've written
a macro to simplify writing operators. The macro uses the '##' operator
to paste together 'operator' and the name of the operator (ie: '+').
gcc reports that pasting the two things together "does not give a valid
preprocessing token". Here's the code:

====================
#define OPER(op) operator ## op

class thing
{
public:

bool OPER(==) (const thing & other) const { return true; }
};


int main() { return 0; }
=============================

The error reads: (line 7): pasting "operator" and "==" does not give a
valid preprocessing token

Any ideas?

Thanks,
--Steve ([email protected])

With gcc 3.4.3 it compiles ok if I omit the ##:
#define OPER(op) operator op

Maett
 
M

mrstephengross

Aha! Ok, so that works. That certainly solves my problem; do you have
any idea why the ## part doesn't seem to work?

--Steve
 
I

Ian

mrstephengross said:
#define OPER(op) operator ## op

class thing
{
public:

bool OPER(==) (const thing & other) const { return true; }
};


int main() { return 0; }
=============================

The error reads: (line 7): pasting "operator" and "==" does not give a
valid preprocessing token

Any ideas?
Looks like a compiler bug, when adding the expected result to the class,
I get

gcc /tmp/x.cc -E

class thing
{
public:

/tmp/x.cc:7:1: pasting "operator" and "==" does not give a valid
preprocessing token
bool operator== (const thing & other) const { return true; }
bool operator== (const thing & other) const { return true; }
};

I think the use of a macro here is questionable, why bother? It just
obscures the code.

Ian
 
M

Maett

mrstephengross said:
Aha! Ok, so that works. That certainly solves my problem; do you have
any idea why the ## part doesn't seem to work?

--Steve

In the GCC preprocessor manual
http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html#Concatenation
they write:
.... Two tokens that don't together form a valid token cannot be pasted
together. For example, you cannot concatenate x with + in either order.
If you try, the preprocessor issues a warning and emits the two tokens.
Whether it puts white space between the tokens is undefined. It is
common to find unnecessary uses of `##' in complex macros. If you get
this warning, it is likely that you can simply remove the `##'...

Maett
 
T

Thomas J. Gritzan

mrstephengross said:
I'm using gcc 3.3.1 to compile the following code (below). I've written
a macro to simplify writing operators. The macro uses the '##' operator
to paste together 'operator' and the name of the operator (ie: '+').

What do you mean by simplifying? Why it would be simplifier to write
OPER(++) instead of operator++?

The ## is for combining two tokens into one token, but "operator=="
don't have to be one token, it is "operator" and "==". Combining them
confuses the compiler.

Thomas
 
G

Greg

mrstephengross said:
Aha! Ok, so that works. That certainly solves my problem; do you have
any idea why the ## part doesn't seem to work?

The rule is that the concatenation produced by the ## operator must be
a valid preprocessing token - even if the concatenation is not used as
such.

The result of the concatenation, operator== is not a valid preprocessor
token because:

#if operator==

is not a legal preprocessor directive. A preprocessor token can
comprise only of letters, digits and the underscore character.

Greg
 
J

Jack Klein

Compiles fine with VC 6.0 and 7.1

/dan

All that proves is that the VC preprocessor has a defect, as far as
the language standard is concerned. The result of the macro expansion
is 'operator==', with no white space, and this is indeed not a valid
preprocessing token. The gcc error message is absolutely correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top