Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Preprocessor concatenation of operator and ...

Reply
Thread Tools

Preprocessor concatenation of operator and ...

 
 
mrstephengross
Guest
Posts: n/a
 
      10-11-2005
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 ((E-Mail Removed))

 
Reply With Quote
 
 
 
 
Dan Cernat
Guest
Posts: n/a
 
      10-11-2005

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 ((E-Mail Removed))


Compiles fine with VC 6.0 and 7.1

/dan

 
Reply With Quote
 
 
 
 
Maett
Guest
Posts: n/a
 
      10-11-2005
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 ((E-Mail Removed))
>
>


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

Maett
 
Reply With Quote
 
mrstephengross
Guest
Posts: n/a
 
      10-11-2005
Aha! Ok, so that works. That certainly solves my problem; do you have
any idea why the ## part doesn't seem to work?

--Steve

 
Reply With Quote
 
Ian
Guest
Posts: n/a
 
      10-11-2005
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
 
Reply With Quote
 
Maett
Guest
Posts: n/a
 
      10-11-2005
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
 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      10-11-2005
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
 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      10-11-2005

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

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      10-12-2005
On 11 Oct 2005 12:04:04 -0700, "Dan Cernat" <(E-Mail Removed)> 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 ((E-Mail Removed))

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
Preprocessor define concatenation michael.martin@asahq.com C Programming 3 01-08-2005 12:36 AM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM
what's the difference between VHDL 93 CONCATENATION and VHDL 87 CONCATENATION? walala VHDL 3 09-18-2003 04:17 AM
Overloading comma to give a concatenation operator: various problems Paul Davis C++ 7 07-17-2003 07:25 AM



Advertisments