Sign in to follow this  

C++ preprocessor hackery -- forcing macro evaluation

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there. I know the C++ preprocessor is highly discouraged and the use of macros is seen as evil. However, I still want to use it to avoid major pain when doing a trivial operation. It's boiler-plate code and I'd be so much better off if I generalized it a bit. The MSVC preprocessor supports this little macro: __COUNTER__. It's supposed to start at 0 and increment every time it's evaluated. This would be very handy: in my boiler-plate code, I wanna declare something (a functor actually) with a unique name, use it, then throw it away. So I'll paste a name and this number together with the ## macro. The problem is that if I use __COUNTER__ multiple times inside the same macro, I get a different number every time! :) Obvious, isn't it? Ok, so what I wanna do is have a helper macro that takes a parameter; the parameter we pass it is __COUNTER__. If this is evaluated *before* it's passed on to the helper macro, we're set (the same number will be used throughout the whole macro). However if it's not, I'll still get multiple __COUNTER__ things inside the helper macro, resulting in different numbers every time it's used. So, forcing a macro's parameter to be evaluated... is it possible?

Share this post


Link to post
Share on other sites
#define YOURMACRO(X) X X
#define HELPER(X) YOURMACRO(X)

HELPER(__COUNTER__)


The code line undergoes a first preprocessor pass which turns it into (assuming the counter has a value of zero):
YOURMACRO(0)

Which then undergoes the second pass of the preprocessor and yields:
0 0

Share this post


Link to post
Share on other sites
ToohrVyk: That's right, the solution was obvious :) What I was doing was a bit too contrived and I managed to screw it up. I see now why I was under the impression that __COUNTER__ wasn't being evaluated properly. Thank you!

stonemetal: I wasn't more specific because I knew someone would point me to boost lambda ;)

Even using that (excellent) library, I'd like a syntax that I can use every day and not worry about too many details. This is what I came up with:



//a function that accepts our lambda function and calls it
template <class F>
void caller_func(F &f) {
cout<<f(10)<<endl;
}

void main() {
//call the function and pass it a lambda function
Let {
float Lambda (float x) {
return x+1;
};
} In caller_func(lambda);

}





Works like a charm. It's just a locally defined functor, the macros are there to make it more readable and to avoid having to come up with a different name every time and changing it in the right places (error prone). I might change the names of the macros to avoid conflicts though.

[EDIT] I can post the macro definitions here if anyone's interested.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this