Sign in to follow this  
Aardvajk

Preprocessor query

Recommended Posts

Anyone got any idea what I am doing wrong here? This is GCC.

#define test(a, b) int a ## b

void f()
{
    test(name, __LINE__);
}
This is creating a variable called "name__LINE__" rather than "name124" or whatever.

I'm trying ultimately to do something like:

#define lock_flag(a) flag_lock_object _temp##__COUNTER__(a)
But I can't seem to get the basics of expanding a macro within a macro working? All web research I've done suggests I'm doing it correctly so driving me a little nuts.

Thanks.

Share this post


Link to post
Share on other sites

Won't this turn __COUNTER__(a) into a string instead of calling macro and turning result into a string?

 

There's some command line parameter to output preprocessed file, that way you can see result you're getting instead of playing guessing game.

Share this post


Link to post
Share on other sites

That's an infamous gotcha of trying to concat macros' contents instead of their names, you just need 2x indirection,

#define test_helper(a, b) int a ## b
#define test(a, b) test_helper(a, b)

void f()
{
    test(name, __LINE__);
}

See for full technicalities: http://stackoverflow.com/questions/1597007/creating-c-macro-with-and-line-token-concatenation-with-positioning-macr

Share this post


Link to post
Share on other sites

Won't this turn __COUNTER__(a) into a string instead of calling macro and turning result into a string?

There's some command line parameter to output preprocessed file, that way you can see result you're getting instead of playing guessing game.


No. Think you are thinking of the # (stringize) operator rather than ## (concatenation) operator.

And I'm not guessing. I can see what is being produced in the errors I get if I create more than one in a scope. But using the flag to inspect is a good idea. Thanks.

Share this post


Link to post
Share on other sites

That's an infamous gotcha of trying to concat macros' contents instead of their names, you just need 2x indirection,

#define test_helper(a, b) int a ## b#define test(a, b) test_helper(a, b)void f(){    test(name, __LINE__);}
See for full technicalities: http://stackoverflow.com/questions/1597007/creating-c-macro-with-and-line-token-concatenation-with-positioning-macr
Thank you. I think I tried that after some googling last night but I will try again based on yours and post the full if it fails again.

[edit] Ah, I think I can see something I didn't do in the link you posted. Cheers, much appreciated.

Share this post


Link to post
Share on other sites
Got it working a treat, thanks.

#define _lock_flag_helper_final(a, b) flag_lock_object _flag_lock_object_##b(a)
#define _lock_flag_helper(a, b) _lock_flag_helper_final(a, b)
#define lock_flag(a) _lock_flag_helper(a, __COUNTER__)

Share this post


Link to post
Share on other sites

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