By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,912 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Preprocessor concatenation of operator and ...

P: n/a
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 (mr************@hotmail.com)

Oct 11 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

mrstephengross wrote:
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 (mr************@hotmail.com)


Compiles fine with VC 6.0 and 7.1

/dan

Oct 11 '05 #2

P: n/a
mrstephengross wrote:
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 (mr************@hotmail.com)


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

Maett
Oct 11 '05 #3

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

--Steve

Oct 11 '05 #4

P: n/a
Ian
mrstephengross wrote:
#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
Oct 11 '05 #5

P: n/a
mrstephengross schrieb:
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
Oct 11 '05 #6

P: n/a
mrstephengross wrote:
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/Co...#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
Oct 11 '05 #7

P: n/a

mrstephengross wrote:
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

Oct 11 '05 #8

P: n/a
On 11 Oct 2005 12:04:04 -0700, "Dan Cernat" <dc*****@excite.com> wrote
in comp.lang.c++:

mrstephengross wrote:
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 (mr************@hotmail.com)


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 12 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.