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)
Related
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.
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;
}
I need to concatenate strings using macros to generate function names.
#define CONCAT(a,b,c) a ## b
int i=1;
CONCAT(a,i)
This code gives ai as a result, while what I wanted is a1.
As there are many functions in my source code, I don't want to enumerate them.
My goal:
for(int i=0;i<100;i++)
{
Funi1();//here i should be from 0 to one hundred
Funi2();
Funi3();
Funi4();
..
}
#Potatoswatter
I have written a script to expand it and the output file cost serval hundred lines.
#Eric Finn
It is not possible since macros are expanded during pre-processing stage.
So, it can not take the value of a variable and concat.
what about __COUNTER__ predefined macro, you have it on GCC and VC. Does it help you?
#include <stdio.h>
#define FUNC2(x,y) x##y
#define FUNC1(x,y) FUNC2(x,y)
#define FUNC(x) FUNC1(x,__COUNTER__)
int FUNC(my_unique_prefix);
int FUNC(my_unique_prefix);
int main() {
my_unique_prefix0 = 0;
printf_s("\n%d",my_unique_prefix0);
my_unique_prefix0++;
printf_s("\n%d",my_unique_prefix0);
}
Example from here
you can pre-compile it with: gcc -E sourcecode.c and watch it, it just replace variable name, and not get value and calculate at all, so it's not possible to let it works like you want.
try it with the LINE macro, it gives u numbers :) if u dont care about 1+1+1+1
like
static int FUNCADD(once,LINE)=1;if(FUNCADD(once,LINE)>0)
if that worx for u
Suppose, I have built an unique function body from below code:
#define TOKENPASTE(x, y) x ## y
#define TOKENPASTE2(x, y) TOKENPASTE(x, y)
#define UNIQUE static void TOKENPASTE2(Unique_, __LINE__)(void)
How can I call this function ?
Macro definition taken from: Creating C macro with ## and __LINE__ (token concatenation with positioning macro).
No. You cannot. Because you cannot determine a function name at runtime. (i.e. either to call Unique_22 or Unique_44. However you can definitely call Unique<22> or Unique<44>)
So you can use template solution instead. Declare Unique as below:
template<unsigned int LINE> void Unique ();
And #define the macro like this:
#define UNIQUE template<> Unique<__LINE__>() {}
I advice to use __COUNTER__ instead of __LINE__ if your compiler supports it.
[Note: which means that in any line you can call the UNIQUE only once and also the macro should be expanded in global or namespace scope (not inside a method).]
After replacing the code with the one given in the answer to the SO question you pointed so that it works, ... you can't call this function directly since you can't know for sure its name, that will change if the code change. I have no clue about how this can be useful in code (maybe scanning an object for symbol like Unique_[0-9]+? Anyway, it would be an indirect use, in code, as said, you can't use it reliably.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Problem with Macros
Hi all
I have defined this macro:
#define SQ(a) (a*a)
and called it in this way:
std::cout << SQ(2+3) << '\n';
the output was 11. Why ?
Thanks
Macros do only simple text manipulation, i.e. they are very stupid that way in that they don't see 'code', only 'text' which are then sent to to the C/C++ parser for validation.
SQ(2+3) becomes (2+3*2+3)
That's why you should use templates, which are a bit smarter, they would do what you expected: first calculate 2+3, then do 5*5.
Because the expansion of SQ in your example gives:
std::cout << (2+3*2+3) << '\n';
A better way to define the macro would be
#define SQ(a) ((a)*(a))
which solves the precedence issue in this case.
Better still would be to use a function which avoids any issues with the passed expression being evaluated more than once.
E.g. as a template:
template<class T>
T SQ(T a) { return a * a; }
Fix your macro to:
#define SQ(a) ((a)*(a))
Others already have the answer. The "fix" (if you must use macros at all) is to wrap all your params with parens, e.g.
#define SQ(a) ((a)*(a))
In most cases, you're better off using templates as this will still perform compile-time expansion for speed, but also provides language support for syntax and type checking.