C++ macro question

Status
Not open for further replies.

Shamyl Zakariya

Ars Praefectus
3,816
Subscriptor++
Say I want to use a macro to expand variants of a function, with different number of arguments.

Such that I might be able to declare:
Code:
FOO( "twoIntParams", int a, int b );
FOO( "oneStringParam", string s );

And get something like:
Code:
void twoIntParams( int a, int b )
{
   // whatever
}

void oneStringParam( string s )
{
  // whatever
}

Note: My plan is not the above, but this seems like an adequate simplification of my scenario.

Is this possible? I'm not a macro expert, in fact I kind of shy away from them except for really simple convenience cases. My impression is that this isn't possible, since I want to treat the parameter lists ( int a, int b ) and ( string s ) as single tokens to substitute. But my understanding of the preprocessor suggests that it will parse them as individual macro arguments.

Trouble is, if I wrap them in quotes when passing them to the macro, say, "int a, int b" the comma won't be parsed, but now I don't know how to "unquote" them when concatting them into my macro.

Thanks,
 

Shamyl Zakariya

Ars Praefectus
3,816
Subscriptor++
bames53":2pc9xhls said:
Say I want to use a macro to expand variants of a function, with different number of arguments.
What's the complication that renders overloading insufficient?

For a lark, I'm writing a signal/slot api (albeit, wrapping boost::bind), and was hoping to use macro expansion to generate sig0, sig1, sig2 type classes from a template, where each version emits a signal with a different number of arguments. Now that I've read up on them, I've realized that variadic macros won't cut it, since I also need to pass parameter placeholders ( like _1, _2, _3 ) to boost::bind.

I'm looking into some other macro approaches which probably won't cut it.

But I've verified in a very simple case function overloading will work, but in the case of user error, will make for some very ugly compiler errors.

Please note: I'm well aware of boost::signal, because I use it! I just want to learn a little by rolling my own simple implementation for fun. For what it's worth, what I've written so far does automatic disconnection for signal receivers derived from my signals::receiver base class... I think it's cool.
 

Shamyl Zakariya

Ars Praefectus
3,816
Subscriptor++
bames53":300xk2in said:
I think the Xcode 4.1 version of clang should support them. You need to use the -std=c++0x flag.

Well, I can't really figure out how to effectively use variadic templates in this context, but I did figure out a clean way to do what I need using macros. I'll revisit this using variadic templates in the future, though.
 

Shamyl Zakariya

Ars Praefectus
3,816
Subscriptor++
Followup:

I've written a macro expansion approach to generate 4 separate signal classes supporting 0 to 3 arguments, and I've also written an overloaded function approach on a single signal class, supporting 0 to 4 arguments. The overloaded function approach strikes me as a much cleaner approach. I'm probably going to stick with that one.

But, you know, I wanted to try both approaches just to be certain.
 

jbode

Ars Legatus Legionis
11,752
Subscriptor
Possible? Probably. Bad Idea? Definitely. The preprocessor is pretty dumb, so you're going to wind up writing something far clunkier than it needs to be.

Having said that, you could do something like

Code:
#define BADIDEA(name, paramlist) \
void name paramlist \
{ \
    //whatever \
}
...
BADIDEA(twoIntParams,(int a, int b));
BADIDEA(oneStringParam,(string c));

For the love of God, don't do this. Just writing it made me feel itchy.
 

Shamyl Zakariya

Ars Praefectus
3,816
Subscriptor++
I went with overloading the signal's () operator with different numbers of arguments, and other appropriate overloads. In the end, when I took both approaches, I intentionally wrote incorrect bindings to see what kind of error messages were generated, and the errors for the macro-generated specializations were worthless. So.

I deleted the macro-based expansion and went with overloading.
 
Status
Not open for further replies.