Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating macro using __LINE__ for different variable names [duplicate]

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.

like image 324
Ivaylo Strandjev Avatar asked Apr 30 '12 07:04

Ivaylo Strandjev


People also ask

How to place all values of a name into a macro?

To place all of the values of the name into a macro variable, we can include a suffix as in the below example. The suffix variable counts the observation (_n_) and appends count onto the macro variable Fname so that unique Macro variables are created for each name.

Why does my macro variable name change before the data step?

Since %LET statements are executed before the data step is even compiled, the macro variable &name will resolve to ‘Name’ as the value and not the actual value in the data set. To overcome this problem we will need to use the data step to place information onto the macro variable symbol tables.

How do you create a macro variable in a SELECT statement?

The INTO clause is used to create the new macro variable NOBS. The colon (:) informs the SELECT statement that the result of the COUNT function is to be written into a macro variable. We can also create macro variables for each of the values as we have created in the data step earlier.

How to assign text value to a macro variable using%let?

%LET statement is followed by the macro variable name, an equal sign ( = ), and then the text value to be assigned to the macro variable. Note that, quotation marks are not used while creating macro variables with the LET statement.


2 Answers

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__);
like image 113
iammilind Avatar answered Oct 11 '22 22:10

iammilind


try this code, I've used it in an older project

#define CONCATENATE_DIRECT(s1, s2) s1##s2
#define CONCATENATE(s1, s2) CONCATENATE_DIRECT(s1, s2)
#ifdef _MSC_VER // Necessary for edit & continue in MS Visual C++.
# define ANONYMOUS_VARIABLE(str) CONCATENATE(str, __COUNTER__)
#else
# define ANONYMOUS_VARIABLE(str) CONCATENATE(str, __LINE__)
#endif 


int ANONYMOUS_VARIABLE(var)

EDIT:

I think you should use COUNTER in visual studio only if also using precompiled headers.

like image 33
Marius Avatar answered Oct 11 '22 22:10

Marius