473,387 Members | 1,641 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,387 software developers and data experts.

G++ paste operator - expands as desired but compile fails

Hello all,

Using g++ 3.3.1 on Linux ("Linux From Scratch") I have a data
structure SCSIParams_t that I wish to print out, field-by-field.
Rather than code a long line of the form

std::cout << "fieldname = " << basepointer->fieldname <<
std::endl;

for every field, I'd like to do it with a short macro, e.g.

EMITSCSI(SCSIDriverType);

I've coded a macro definition

#define EMITSCSI(fieldname) \
std::cout << # field << " = " << basepointer->##fieldname <<
std::endl

and coded, as a test, a single line identical to the "short macro,
e.g..." example shown above.

That line generates the compile error,

pasting "->" and "fieldname" does not give a valid
preprocessing token

However, using the -E switch on the compile line, I find that the
macro does, in fact, expand to a line identical to the "long line..."
shown above. In fact, if I cut-and-paste the expansion text from the
preprocessor output into my source file, and comment out the line that
uses the macro, my code compiles cleanly. That is to say, "expanding
the macro" MANUALLY, i.e. "by hand," leads to successful compilation,
but letting the preprocessor do it leads to failiure-to-compile.

Can someone suggest a way around this? I also have g++ 3.4.3
available, although that's not what we use for standard development.

Thanks,

Chris

PS - the e-mail address appearing on this message is no longer valid,
so don't e-mail. My new address is similar but different:
cc******@twcny.rr.com . I'll create a new Google Groups login soon...

Aug 15 '07 #1
4 2243
On 15 Srp, 19:35, Chris <cchie...@rochester.rr.comwrote:
Hello all,

Using g++ 3.3.1 on Linux ("Linux From Scratch") I have a data
structure SCSIParams_t that I wish to print out, field-by-field.
Rather than code a long line of the form

std::cout << "fieldname = " << basepointer->fieldname <<
std::endl;

for every field, I'd like to do it with a short macro, e.g.

EMITSCSI(SCSIDriverType);

I've coded a macro definition

#define EMITSCSI(fieldname) \
std::cout << # field << " = " << basepointer->##fieldname <<
std::endl

and coded, as a test, a single line identical to the "short macro,
e.g..." example shown above.

That line generates the compile error,

pasting "->" and "fieldname" does not give a valid
preprocessing token

However, using the -E switch on the compile line, I find that the
macro does, in fact, expand to a line identical to the "long line..."
shown above. In fact, if I cut-and-paste the expansion text from the
preprocessor output into my source file, and comment out the line that
uses the macro, my code compiles cleanly. That is to say, "expanding
the macro" MANUALLY, i.e. "by hand," leads to successful compilation,
but letting the preprocessor do it leads to failiure-to-compile.

Can someone suggest a way around this? I also have g++ 3.4.3
available, although that's not what we use for standard development.

Thanks,

Chris

PS - the e-mail address appearing on this message is no longer valid,
so don't e-mail. My new address is similar but different:
cchie...@twcny.rr.com . I'll create a new Google Groups login soon...
1. Delete extra white-space between # and field
2. Delete ## - it is not necessary. -and member name are different
preprocessor tokens, so you do not need to concat them with ##
#define EMITSCSI(fieldname) \
std::cout << #field << " = " << basepointer->fieldname <<
std::endl

Aug 15 '07 #2
On Aug 15, 10:35 am, Chris <cchie...@rochester.rr.comwrote:
Hello all,

Using g++ 3.3.1 on Linux ("Linux From Scratch") I have a data
structure SCSIParams_t that I wish to print out, field-by-field.
Rather than code a long line of the form

std::cout << "fieldname = " << basepointer->fieldname <<
std::endl;

for every field, I'd like to do it with a short macro, e.g.

EMITSCSI(SCSIDriverType);

I've coded a macro definition

#define EMITSCSI(fieldname) \
std::cout << # field << " = " << basepointer->##fieldname <<
std::endl
Don't bother with ## here. It's only needed when you want to combine
two preprocessor tokens into one.

I.e. you want to create a new identifier. Since -and fieldname are
two separate tokens anyways, just go with

std::cout << #fieldname " = " << basepointer->fieldname <<
std::endl;

Note that I corrected your typo, and that I elided the (unneeded) <<
between #field and " = ". Since they're both string literals, the
compiler will concatenate them.

You might want to look into overloading operator<< for SCSIParams_t,
i.e.

std::ostream& operator<<(std::ostream&, const SCSIParams_t&);

>
and coded, as a test, a single line identical to the "short macro,
e.g..." example shown above.

That line generates the compile error,

pasting "->" and "fieldname" does not give a valid
preprocessing token
Aug 15 '07 #3
On Wed, 15 Aug 2007 10:35:30 -0700 in comp.lang.c++, Chris
<cc******@rochester.rr.comwrote,
>
#define EMITSCSI(fieldname) \
std::cout << # field << " = " << basepointer->##fieldname <<
std::endl

and coded, as a test, a single line identical to the "short macro,
e.g..." example shown above.

That line generates the compile error,

pasting "->" and "fieldname" does not give a valid
preprocessing token
Read the message. It contains the explanation of what the problem is.
In fact, pasting "->" can never begin a larger preprocessing token.
The pasting is extraneous, you want simply instead
<< basepointer -fieldname

By the way, you are committing endl abuse. Use << '\n'

Aug 15 '07 #4

Thanks to all who replied. To paraphrase and respond a little,
#define EMITSCSI(fieldname) \
std::cout << # field << " = " << basepointer->##fieldname <<
std::endl

[...]
pasting "->" and "fieldname" does not give a valid
preprocessing token

Read the message. It contains the explanation of what the problem is.
With all due respect, if I understood what the error message was
talking about, I would have fixed the problem instead of posting. :-)
In fact, pasting "->" can never begin a larger preprocessing token.
I don't understand what you mean by that. I expected the preprocessor
to paste together whatever I told it to paste together, and pass the
result along to the compiler. You're saying that's not the way it
works?
The pasting is extraneous, you want simply instead
<< basepointer -fieldname
I'm amazed that it's as simple as that. Clearly the scope-and-
behavior of tokens within a macro definition is a completely "other
animal" than in ordinary code. This is why I've programmed for 18+
years WITHOUT USING macros. Can never get 'em to do what I want, and
spend an inordinate amount of time fighting with the compiler over the
matter.
By the way, you are committing endl abuse. Use << '\n'
Another shock. Every C++ book I've ever read instructs the reader to
use 'endl' (or, worse, 'std::endl') to generate end-of-line. Did this
change? When?

Chris

Aug 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
3
by: Prakash Bande | last post by:
Hi, I have bool operator == (xx* obj, const string st). I have declared it as friend of class xx. I am now able to do this: xx ox; string st; if (&ox == st) { } But, when I have a vector<xx*>...
7
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
1
by: John Doe | last post by:
I collapse an outline block, then copy it, then paste it. In my experience, the outline block always expands after the paste. Is their a way to modify the editor's behavior so that an outline block...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
2
by: Arvid Requate | last post by:
Hello, I'd like to understand why the following code does not compile. It looks like a strangeness in connection with overload resolution for the <complex> header: The conversion operator...
6
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a...
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.