Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with macro expansion

Reply
Thread Tools

Problem with macro expansion

 
 
me
Guest
Posts: n/a
 
      11-09-2004
Hi guys

I want to insert a load of pieces of data into a map

The map has an std::string representing a field name as the key, and the
value is a struct with 2 members - the field length and a bool
indicating whether the field is a special field or not.

The struct representing the value has a constructor that takes a single
parameter for the length, and defaults the boolean to false.

To simplify the code, and make things look neater, I defined a macro:

#define SETUP_FIELD(FIELD_NAME, FIELD_SIZE)
FieldMap.insert(std::make_pair(std::string("FIELD_ NAME"),FieldData(FIELD_SIZE)))
;

So i can do:

SETUP_FIELD (TheFirstField,3) ;
SETUP_FIELD (TheSecondField,1) ;
SETUP_FIELD (TheThirdField,2) ;
SETUP_FIELD (TheFourthField,7) ;

etc...

But when i look at the map, all the keys are set to "FIELD_NAME", rather
than "TheFirstField" etc - the macro has not substituted the string I
pass in. I guess this is something to do with substitution within a
string literal...but I'm a bit stumped.

Any Ideas?

dtw
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-09-2004
me wrote:
> I want to insert a load of pieces of data into a map
>
> The map has an std::string representing a field name as the key, and the
> value is a struct with 2 members - the field length and a bool
> indicating whether the field is a special field or not.
>
> The struct representing the value has a constructor that takes a single
> parameter for the length, and defaults the boolean to false.
>
> To simplify the code, and make things look neater, I defined a macro:
>
> #define SETUP_FIELD(FIELD_NAME, FIELD_SIZE)
> FieldMap.insert(std::make_pair(std::string("FIELD_ NAME"),FieldData(FIELD_SIZE)))


FieldMap.insert(std::make_pair(std::string(#FIELD_ NAME), \
FieldData(FIELD_SIZE)));

RTFM on the '#' stringizing operator.

> ;
>
> So i can do:
>
> SETUP_FIELD (TheFirstField,3) ;
> SETUP_FIELD (TheSecondField,1) ;
> SETUP_FIELD (TheThirdField,2) ;
> SETUP_FIELD (TheFourthField,7) ;
>
> etc...
>
> But when i look at the map, all the keys are set to "FIELD_NAME", rather
> than "TheFirstField" etc - the macro has not substituted the string I
> pass in. I guess this is something to do with substitution within a
> string literal...


Yes, there is no such substitution.

>but I'm a bit stumped.
>
> Any Ideas?


See above.

V
 
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
partial/selective Macro expansion scpp reppisch C Programming 7 05-07-2006 05:27 PM
Macro expansion of '#__LINE__'? Dom Gilligan C Programming 4 11-04-2005 05:47 PM
Macro Expansion Vittal C Programming 3 03-23-2005 02:04 PM
Macro expansion: intercept statement interpretation Benjamin Niemann Python 3 08-26-2004 02:51 AM
A question on macro expansion Ark C Programming 3 07-22-2004 11:22 PM



Advertisments