[Macro] Token concatenation operator ##

Hi there,

I'm still not able to understand how to use the token merging operator ## in C/C++ macros. Here I'd like to concatenate the actual term `profiler.' with the argument of the macro `stmt':
 
#define PROFILER(stmt) profiler.##stmt 

The idea is that a macro such as `PROFILER(search_start());' would yield the text `profiler.search_start();'
Still the above code returns the following error in linux/clang5:
1
2
3
4
5
xxx.cpp:362:5: error: pasting formed '.search_start', an invalid preprocessing token
    PROFILER( search_start() );
    ^
xxx.cpp:182:33: note: expanded from macro 'PROFILER'
#define PROFILER(stmt) profiler.##stmt 

What's the proper syntax to use this feature in this scenario?

Thanks,
Dean
Last edited on
I think you have to concatenate the token with chars allowed in identifiers. So you could add a leading underscore to the struct member names and then do this:
#define PROFILER(stmt) profiler._##stmt
ok, so the ## is not required in this case, i.e. :
#define PROFILER(stmt) profiler.stmt
The above definition is accepted by the preprocessor .

Topic archived. No new replies allowed.