473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Preprocessor concatenation of operator and ...

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
8 3547

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: Cronus | last post by:
Hi the following code the g++ (g++ (GCC) 3.3.3 (Debian 20040422)) emits the error message that pasting of :: and hello is no valid preprocessor token. The g++ 2.95.3 accepts the code. I know...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
3
by: michael.martin | last post by:
Hi, I am wondering if something like this is possible? #define COLON ":" #define PERIOD "." #define WORD "word" #define WORD_COLON WORD+COLON
23
by: Bonj | last post by:
what is the correct form of string concatenation in VB.NET, + or &? Both seem to work, but which is correct? I know it's + in C# and & in VB6, but which in VB.NET?
2
by: Prashant Mahajan | last post by:
I just wanted comments from you all on the following topic: Let's say we have 2, C code files namely file1.c and file2.c. file1.c contains few pre-processor definations say #define TEST1 10 We...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
1
by: Guenter Dannoritzer | last post by:
Hi, I have a piece of code that got developed using Microsoft .NET 2003 and I try to compile it with g++/gcc 4.1.2. There is a problem with the string concatenation operator for the...
4
by: Mamluk Caliph | last post by:
For quite some time I've used code as the following to be able to "cut&paste" parts from different headerfiles of the same name. It has worked with GCC, MS, Borland, Keil to name a few. In...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.