C++ – Creating macro using __LINE__ for different variable names

cc-preprocessorconcatenationmacrosvisual studio 2010

Possible Duplicate:
Creating C macro with ## and LINE (token concatenation with positioning macro)

I am trying to use the __LINE__ macro to generate different variable names. I have a scoped benchmark class called Benchmark(located in the utils namespace) and it's constructor takes a string. Here is the macro definition I have created:

#define BENCHMARK_SCOPE utils::Benchmark bm##__LINE__(std::string(__FUNCTION__))

Unfortunately this causes the following error:

<some_file_name>(59): error C2374: 'bm__LINE__' : redefinition; multiple initialization

This leads me to the conclusion the __LINE__ macros does not get expanded. I have created my macross according to this post. Do you have ideas why __LINE__ does not get expanded?

EDIT: probably the compiler info is also relevent. I am using visual studio 2010.

Best Answer

You need to use combination of 2 macros:

#define COMBINE1(X,Y) X##Y  // helper macro
#define COMBINE(X,Y) COMBINE1(X,Y)

And then use it as,

COMBINE(x,__LINE__);
Related Topic