473,237 Members | 1,392 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,237 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 2240
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;
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.