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

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 1597
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
3
by: John | last post by:
Please, consider this macro: #define mymacro(arg1, arg2) arg1 and arg2 Then it is used: mymacro(boys, girls) How is its expansion?
10
by: Karim Thapa | last post by:
Why following macro does not work? #define removebrace(x) x void Foo(int a, int b, char *txt, int d, int e); main() {
7
by: reppisch | last post by:
Hi Ng, i am looking for a method of expanding a macro while the rest of the code remains untouched. I have some code which does macro voodo / ifdef's which i would like to strip and simplify. ...
4
by: ImOk | last post by:
I come from the Visual Foxpro world, which is one reason I love PHP. VFP is a scripting type language with macro substitution abilities similar to PHP. Besides the regular expansion I can do...
2
by: talkaboutquality | last post by:
Need to define a macro as, say, #ifdef NEED_FUNCTION foo(x) #else #endif
6
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in...
5
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing...
16
by: mdh | last post by:
I have asked a few questions about Macros...and think what I have been missing ...and which all who have replied clearly understand...is that this is a Pre-processor action. Hopefully the above is...
4
by: nakiya | last post by:
I wrote a simple macro for printing a message on a line. But I get a strange error by the compiler. Can someone help plz. #define Notify(comm)\ ({cout << (comm) << endl;}) . . . const char*...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
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...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
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"....

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.