在C++里能不可不通过宏接收数目不定的参数
在C++里能不能不通过宏接收数目不定的参数?
有没有能放不同类型的变量的容器?其他的实现方法也行。
------解决方案--------------------
#define a(...) printf(__VA_ARGS__)
容器的话可以用tuple
------解决方案--------------------
Visual C++ Language Reference
Variadic Macros
Variadic macros are function-like macros that contain a variable number of arguments.
Remarks
To use variadic macros, the ellipsis may be specified as the final formal argument in a macro definition, and the replacement identifier __VA_ARGS__ may be used in the definition to insert the extra arguments. __VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them.
The C Standard specifies that at least one argument must be passed to the ellipsis, to ensure that the macro does not resolve to an expression with a trailing comma. The Visual C++ implementation will suppress a trailing comma if no arguments are passed to the ellipsis.
Support for variadic macros was introduced in Visual C++ 2005.
Example
Copy Code
// variadic_macros.cpp
#include <stdio.h>
#define EMPTY
#define CHECK1(x, ...) if (!(x)) { printf(__VA_ARGS__); }
#define CHECK2(x, ...) if ((x)) { printf(__VA_ARGS__); }
#define CHECK3(...) { printf(__VA_ARGS__); }
#define MACRO(s, ...) printf(s, __VA_ARGS__)
int main() {
CHECK1(0, "here %s %s %s", "are", "some", "varargs1(1)\n");
CHECK1(1, "here %s %s %s", "are", "some", "varargs1(2)\n"); // won't print
有没有能放不同类型的变量的容器?其他的实现方法也行。
------解决方案--------------------
#define a(...) printf(__VA_ARGS__)
容器的话可以用tuple
------解决方案--------------------
Visual C++ Language Reference
Variadic Macros
Variadic macros are function-like macros that contain a variable number of arguments.
Remarks
To use variadic macros, the ellipsis may be specified as the final formal argument in a macro definition, and the replacement identifier __VA_ARGS__ may be used in the definition to insert the extra arguments. __VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them.
The C Standard specifies that at least one argument must be passed to the ellipsis, to ensure that the macro does not resolve to an expression with a trailing comma. The Visual C++ implementation will suppress a trailing comma if no arguments are passed to the ellipsis.
Support for variadic macros was introduced in Visual C++ 2005.
Example
Copy Code
// variadic_macros.cpp
#include <stdio.h>
#define EMPTY
#define CHECK1(x, ...) if (!(x)) { printf(__VA_ARGS__); }
#define CHECK2(x, ...) if ((x)) { printf(__VA_ARGS__); }
#define CHECK3(...) { printf(__VA_ARGS__); }
#define MACRO(s, ...) printf(s, __VA_ARGS__)
int main() {
CHECK1(0, "here %s %s %s", "are", "some", "varargs1(1)\n");
CHECK1(1, "here %s %s %s", "are", "some", "varargs1(2)\n"); // won't print