Jump to content
• Advertisement

Preprocessor query

This topic is 1403 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

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

Share on other sites
Advertisement

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

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__);
}


Share this 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

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.

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

Share this 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

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
2. 2
Rutin
22
3. 3
JoeJ
20
4. 4
5. 5
• Advertisement

• 27
• 40
• 23
• 13
• 13
• Forum Statistics

• Total Topics
631735
• Total Posts
3001941
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!