Macro question (C++)

Started by
12 comments, last by d000hg 17 years, 11 months ago
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)
Advertisement
I think you want to do this.

#define CREATE_FUNCTOR(name,method) struct _##name { void method(); };
Gross. Prefixing identifers with an underscore is naughty. But what you want is ##, the token pasting operator.

#define foo(a) _##a    // foo(bar) --> _bar
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).
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.
Quote:Original post by Sneftel
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.


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.
Quote:Original post by d000hg
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!


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...
Quote:Original post by Zahlman
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...


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]

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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]
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]

This topic is closed to new replies.

Advertisement