468,719 Members | 1,940 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,719 developers. It's quick & easy.

Problem with macro expansion

me
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
Jul 22 '05 #1
1 1429
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
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by Andrew Dalke | last post: by
3 posts views Thread by John | last post: by
10 posts views Thread by Karim Thapa | last post: by
7 posts views Thread by reppisch | last post: by
2 posts views Thread by talkaboutquality | last post: by
6 posts views Thread by jason | last post: by
5 posts views Thread by Francois Grieu | last post: by
16 posts views Thread by mdh | last post: by
4 posts views Thread by nakiya | last post: by
1 post views Thread by Oskars | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.