Bulky macro definition overload
c, c++, macros
Solution
You can have a variable number of arguments in your macro. They can be accessed by using special symbols like `__VA_ARGS__` inside the macro.
Here's the syntax in standard C.
#define DECLARE_FUNCTION(funcName, retType, ...) retType funcName(__VA_ARGS__)
The `...` stands for all the dynamic arguments and is accessed by `__VA_ARGS__`. Note that you need at least one dynamic argument, otherwise you get a compiler error.
GNU C++ introduces extensions to prevent this from happening. So you can alternatively declare the above as:
#define DECLARE_FUNCTION(funcName, retType, ...) retType funcName(##__VA_ARGS__)
Here are some examples:
`DECLARE_FUNCTION(func1, void)` becomes `void func1()` (only with extensions).
`DECLARE_FUNCTION(func2, int, int, char)` becomes `int func2(int, char)`
This feature is called "variadic macros". You can read more here.
Problem
I want to define macro like ``` #define DECLARE_FUNCTION(funcName, retType, args) retType funcName(args) ``` And use it like ``` DECLARE_FUNCTION(intFunc, int, void); DECLARE_FUNCTION(voidFunc, void, double, double); DECLARE_FUNCTION(doubleFunc, double, int, double, double); ``` expecting that those will expand to ``` int intFunc(void); void voidFunc(double, double); double doubleFunc(int, double, double); ``` This is certainly not working, as macro defined with three arguments eats all the “redundant” arguments and result is ``` int intFunc(void); void voidFunc(double); double doubleFunc(int); ``` I don’t mind defining macros for different cases, like `DECLARE_FUNCTION_WITH_0_ARGS`, `DECLARE_FUNCTION_WITH_1_ARG`, `DECLARE_FUNCTION_WITH_2_ARGS`, etc. But the problem is that these macros are not as primitive as I gave in the example, they contain a lot of lines of code, and it would be nice not to rewrite them, but to define only one nontrivial macro, e.q. `DECLARE_FUNCTION_WITH_1_ARG`, and call it from bodies of all other macros.