a macro substitution involving concatenation and line number [duplicate] - c++

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;
}

Related

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.

How does #define work in C++ [duplicate]

This question already has answers here:
Macro Expansion
(7 answers)
The need for parentheses in macros in C [duplicate]
(8 answers)
Closed 4 years ago.
#include <iostream>
using namespace std;
#define squareOf(x) x*x
int main() {
// your code goes here
int x;
cout<<squareOf(x+4);
return 0;
}
I thought the answer would come as 16 but it came out as 4.
I am confused how does this works.
16 would never be the result here. Let's say you would have initialized x with 0, then you would have your x+4 replaced by x+4*x+4 which would evaluate as 0+4*0+4 = 4.
Preprocessor macros replace source code, they are not functions.
You might now think that maybe
#define squareOf(x) (x)*(x)
would be better, but consider that then
int x = 2;
int y = squareOf(x++);
would result in y = (2)*(3) = 6, not in 4.
If you do not have a really good reason, avoid preprocessor macros. There are good reasons, but if something behaves like a function, better make it a function.
Now take a look at this:
template <class T>
inline T squareOf(const T& number)
{
return number*number;
}
As inline, it does also replace code (at least if the compiler wants so), but here, this one actually behaves like a function (since it is one). Wouldn't expect a bad outcome from that one.

How to token paste a number? [duplicate]

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.

C++ macro with variable number of arguments [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C/C++: How to make a variadic macro (variable number of arguments)
I need macro that will expand in a array that contains it's arguments. For example:
#define foo(X0) char* array[1] = {X0}
#define foo(X0, X1) char* array[2] = {X0, X1}
and so on. My problem is that I need to use foo with variable number of arguments, so I want to be able to use foo("foo0") but also to use foo("foo0", "foo1", "foo2"..."fooN"). I know it's possible to have:
#define foo(...)
#define foo_1(X0) ..
#define foo_2(X0, X1) ..
#define foo_3(X0, X1, X2) ..
#define foo_N(X0, X1, ... XN) ..
And use ____VA_ARGS____, but I don't know how may I expand foo in foo_k macro depending on it's parameter count? Is this possible?
How about:
#define FOO( ... ) char* x[] = { __VA_ARGS__ };
This should work:
#define foo(args...) char* array[] = {args}
Note that this uses a GNU extension and so will only work with gcc and gcc-compatible compilers. #JoeSlav's answer using __VA_ARGS__ is more portable.
I recommend gcc.gnu.org docs on the subject.
Or you could jump straight away to this answer:
How to make a variadic macro (variable number of arguments)