Question

I've got the following Macro to define a new function following a special naming scheme:

#define CREATE_HOOK_STUB( func ) void ##func_STUB() { /* some code*/ }

However, the preprocessor always concatenates void and ##func_STUB but I obviously want it to preserve the whitespace at that position.

I know that I could just prepend some string to ##func_STUB, but this is not what I want. How would I solve this?

Was it helpful?

Solution

I think what you really want is closer to:

#define CREATE_HOOK_STUB( func ) void func##_STUB() { /* some code*/ }

But you should also look at this linked SO answer which has some additional details on the behavior of the token pasting operator:

One thing to be aware of when you're using the token-paste ('##') or stringizing ('#') preprocessing operators is that you have to use an extra level of indirection for them to work properly in all cases.

If you don't do this and the items passed to the token-pasting operator are macros themselves, you'll get results that are probably not what you want...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top