Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > G++ paste operator - expands as desired but compile fails

Reply
Thread Tools

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

 
 
Chris
Guest
Posts: n/a
 
      08-15-2007
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:
http://www.velocityreviews.com/forums/(E-Mail Removed) . I'll create a new Google Groups login soon...

 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      08-15-2007
On 15 Srp, 19:35, Chris <(E-Mail Removed)> wrote:
> 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:
> (E-Mail Removed) . 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

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      08-15-2007
On Aug 15, 10:35 am, Chris <(E-Mail Removed)> wrote:
> 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:stream& operator<<(std:stream&, 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
>


 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      08-15-2007
On Wed, 15 Aug 2007 10:35:30 -0700 in comp.lang.c++, Chris
<(E-Mail Removed)> wrote,
>
> #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'

 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      08-20-2007

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

 
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
Expand/Collapse script expands but does not collapse M.L. Javascript 9 08-29-2007 08:49 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
Microsoft Signs on as Elite Sponsor of XML 2004; Program Expands Marion Elledge XML 0 11-10-2004 07:46 PM
XML 2004 Program Expands -- Register Today! MarionEll XML 0 11-02-2004 07:46 PM
Why? (table expands to width of text in textbox) ck HTML 5 06-08-2004 05:52 PM



Advertisments