Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c/c++ (VS2008) enclose macro param in quotes

For a lot of function calls in a C app that needs some degree of debugging I wanted to add a macro to ease the typing that I had to do.

right now I am calling a function like this:

aDebugFunction(&ptrToFunction, __LINE__, "ptrToFunction", param1, param2, etc)

So I thought lets write a macro that does the first 3 parameters for me, like this:

#define SOMEDEFINE(x) &x, __LINE__,  "x"

However, as most of you will immediately know, this won't work it won't replace "x" with the name that x has been given but will just pass "x" as 3rd parameter.

My knowledge of this preprocessor macro happening stuff is quite limited and thus my googling-ability is also quite useless due to not knowing where to search for exactly.

I hope one of you guys/girls could give me either a solution or point me in the right direction.

like image 940
Daan Timmer Avatar asked Mar 02 '11 10:03

Daan Timmer


People also ask

What does ## mean in macro?

The double-number-sign or token-pasting operator (##), which is sometimes called the merging or combining operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token, and therefore, can't be the first or last token in the macro definition.

How to write macro in c++?

Macros and its types in C/C++Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions need not be terminated by a semi-colon(;).

Can we assign macro to a variable in C?

Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.

What is Stringify in C++?

“Stringification” means turning a code fragment into a string constant whose contents are the text for the code fragment. For example, stringifying foo (z) results in “foo (z)” . In the C & C++ preprocessor, stringification is an option available when macro arguments are substituted into the macro definition.


1 Answers

You need to use the # convert token to string command of the preprocessor. You should define your second macro this way:

#define SOMEDEFINE(x) &x, __LINE__,  # x

Or if x can also be a macro call, and you want the string to contains the expansion of the macro, you need to use an auxiliary macro:

#define TOKEN_TO_STRING(TOK) # TOK
#define STRINGIZE_TOKEN(TOK) TOKEN_TO_STRING(TOK)
#define SOMEDEFINE(x) &x, __LINE__, STRINGIZE_TOKEN(x)

For example, if you have the following code:

#define SHORT_NAME a_very_very_very_long_variable_name
SOMEDEFINE(SHORT_NAME)

Then, with the first macro, it will expand to

&a_very_very_very_long_variable_name, __LINE__, "SHORT_NAME"

While, with the second macro, it will expand to:

&a_very_very_very_long_variable_name, __LINE__, "a_very_very_very_long_variable_name"
like image 71
Sylvain Defresne Avatar answered Oct 15 '22 02:10

Sylvain Defresne