# Macro question (C++)

This topic is 4418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a macro which is something like this:
#define CREATE_FUNCTOR(name,method) struct name {  void method(); };
This isn't exact so don't focus on why I want to do this! My problem is that I want the struct to be called _name, not name. If I change the macro to be:
#define CREATE_FUNCTOR(name,method) struct _name {  void method(); };
Then I get a struct called "_name". Is it possible to get the preproccesor to understand I want to stick '_' onto the front of the variable? (It's actually for a message handling thing and I want to create a functor called _OnXXX for each event-handler OnXXX)

##### Share on other sites
I think you want to do this.

#define CREATE_FUNCTOR(name,method) struct _##name { void method(); };

##### Share on other sites
Gross. Prefixing identifers with an underscore is naughty. But what you want is ##, the token pasting operator.

#define foo(a) _##a    // foo(bar) --> _bar

##### Share on other sites
Thanks guys. I agree it's ugly to use macros for this at all but I don't think C++ makes what I want possible through normal means (I'm making something a little like MFC).

##### Share on other sites
It is in particular naughty to just use an underscore, though, because it is extremely likely that someone using your framework will want to use that name. Don't make it _whatever. At the very least, make it __AUTO__d000hgsframework__whatever or suchlike.

##### Share on other sites
Quote:
 Original post by SneftelIt is in particular naughty to just use an underscore, though, because it is extremely likely that someone using your framework will want to use that name. Don't make it _whatever. At the very least, make it __AUTO__d000hgsframework__whatever or suchlike.

He should be careful with the CREATE_FUNCTOR identifier as it's a macro, but the _name identifier is fine, as it obeys scoping rules.

Also, the identifier __AUTO__d000hgsframework__ is reserved to the compiler and standard library.

##### Share on other sites
Quote:
 Original post by d000hgI have a macro which is something like this:#define CREATE_FUNCTOR(name,method) struct name { void method(); };This isn't exact so don't focus on why I want to do this!

Sorry, I must insist. You are aware of std::mem_fun and std::mem_fun_ref, yes? I'm pretty sure there's *something* that solves your problem more nicely...

##### Share on other sites
Quote:
 Original post by ZahlmanSorry, I must insist. You are aware of std::mem_fun and std::mem_fun_ref, yes? I'm pretty sure there's *something* that solves your problem more nicely...

And if those don't do what you need, how about boost::function and boost::bind?
I can guarrentee they will do more than you can do with your macros [grin]

##### Share on other sites
Quote:
 Original post by Nitage[...]Also, the identifier __AUTO__d000hgsframework__ is reserved to the compiler and standard library.

</sarcasm>.

May I conclude that you think using identifiers like this is OK, in spite of the holy?

To come out, yes, I use #ifndef __MYFREAKINGFILENAME_H... all the time. [smile]

##### Share on other sites
Quote:
Original post by Konfusius
Quote:
 Original post by Nitage[...]Also, the identifier __AUTO__d000hgsframework__ is reserved to the compiler and standard library.

</sarcasm>.

May I conclude that you think using identifiers like this is OK, in spite of the holy?

To come out, yes, I use #ifndef __MYFREAKINGFILENAME_H... all the time. [smile]

Why type 2 extra characters just to risk breaking your program? [smile]

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 37
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631413
• Total Posts
2999945
×