How to token paste a number? [duplicate] - c++

This question already has answers here:
How can I concatenate twice with the C preprocessor and expand a macro as in "arg ## _ ## MACRO"?
(3 answers)
Closed 7 years ago.
I have to create objects dynamically. So for that I have the following:
#define timerID(num) timerID_##num
This results in as timerID_num instead of say timerID_1.
Can someone let me know how to do this?

Check following code snippet:
#define f(g,g2) g##g2
void main()
{
int timerID_1 = 12;
printf("%d",f(timerID_,1));
}
This will concatenate to timerID_1. I printed the value just for debug.

Related

Parse int from #define C++ [duplicate]

This question already has answers here:
C++ string to enum
(13 answers)
How to easily map c++ enums to strings
(23 answers)
Closed 1 year ago.
Assume the following code:
#define GL_DEPTH_TEST 0x0B71
std::string shader_args = "GL_DEPTH_TEST";
I want to run something like
glEnable(shader_args);
//Draw stuff
glDisable(shader_args);
I have tried std::atoi(shader_args) but it returns 0 instead of 0x0B71

Can someone please explain why the output of this code is 3 and not 4? [duplicate]

This question already has answers here:
Unexpected Result in Macro
(2 answers)
Closed 2 years ago.
Can someone please explain why the output of this code is 3 and not 4?
#define square(x) (x*x)
int main () {
int x,y=1;
x=square(y+1);
printf("%d\n",x);
return 0;
}
The reason is that what preprocessor does about macros is quite like a search-replace. So you get y+1*y+1 which gives three. To avoid such problems
wrap every variable in macro definition with parentheses #define square(x) ((x)*(x))
use functions instead (prefered as less likely to run into random errors)

do{}while(0) vs an empty statement [duplicate]

This question already has answers here:
do { ... } while (0) — what is it good for? [duplicate]
(5 answers)
Why use apparently meaningless do-while and if-else statements in macros?
(9 answers)
Closed 4 years ago.
Trying to make a logging system that only logs data when a certain macro is defined, I've done something like this:
#ifdef _DEBUG
#define foo(a) std::cout << a << std::endl
#else
#define foo(a)
#endif
int main()
{
foo("Hello!");
return 0;
}
The function main, after pre-processing, expands to:
int main()
{
;
return 0;
}
However, on some places, I saw that people use do{}while(0) instead of an empty macro. I suppose that a compiler would optimize away both of these but I'm wondering is there an advantage that one has over another?
I am aware of the need for both an empty statement and do{}while(0) but what I do not know is the difference between the two.
I don't believe my question was fully read and compared to the ones that have been provided when marking as duplicate.

Is it possible to stringify a variadic macro into a comma delimited list of strings? [duplicate]

This question already has answers here:
Foreach macro on macros arguments
(7 answers)
Closed 4 years ago.
In essence I want to write
MACRO(a, b, c)
and have it result in
"a","b","c"
I tried using #__VA_ARGS__, but it results in one string containing all arguments:
"a, b, c"
which is not what I want.
#define MACRO3(a, b, c) #a,#b,#c
Make one for each argument count you might use

a macro substitution involving concatenation and line number [duplicate]

This question already has answers here:
Creating C macro with ## and __LINE__ (token concatenation with positioning macro)
(3 answers)
Closed 6 years ago.
I would like to have a macro that produce something like L17, where 17 is the line number when the macro is invoked. However the following only produce L__LINE__
#define STOP L##__LINE__
Wonder if there is a way to make the __LINE__ evaluate before concatenation.
You need a double concat macro wrapper:
#define CONCAT0(x,y) x ## y
#define CONCAT(x,y) CONCAT0(x,y)
#define STOP CONCAT(L,__LINE__)
int main() {
int STOP = 42;
L5 = 41;
}