variadic-macros

Are Variadic macros nonstandard?

*爱你&永不变心* 提交于 2019-11-26 14:48:15
问题 For debugbuilds, I usually use Clang, as it formats warnings and errors better, and makes it a little easier to track them down, and fix them. But recently after adding a Macro with variadic arguments, Clang told me the following (from a dummy project): main.cpp:5:20: warning: named variadic macros are a GNU extension [-Wvariadic-macros] #define stuff3(args...) stuff_i(args) I know that macroname(args...) compiles fine in a wide range of compilers, including Visualstudio, Sunstudio, and of

MSVC doesn't expand __VA_ARGS__ correctly

匆匆过客 提交于 2019-11-26 13:20:58
Consider this code: #define F(x, ...) X = x and VA_ARGS = __VA_ARGS__ #define G(...) F(__VA_ARGS__) F(1, 2, 3) G(1, 2, 3) The expected output is X = 1 and VA_ARGS = 2, 3 for both macros, and that's what I'm getting with GCC, however, MSVC expands this as: X = 1 and VA_ARGS = 2, 3 X = 1, 2, 3 and VA_ARGS = That is, __VA_ARGS__ is expanded as a single argument, instead of being broken down to multiple ones. Any way around this? MSVC's preprocessor seems to behave quite differently from the standard specification. Probably the following workaround will help: #define EXPAND( x ) x #define F(x, ...

Can macros be overloaded by number of arguments?

喜夏-厌秋 提交于 2019-11-26 11:56:04
问题 How does this work? How can a C99/C++11 variadic macro be implemented to expand to different things on the sole basis of how many arguments are given to it? 回答1: (Edit: See the end for a ready-made solution.) To get an overloaded macro, first we need a macro which selects between several implementations. This part doesn't use a variadic macro. Then a variadic macro which generically counts its arguments produces a selector. Plugging the argument count into a dispatcher produces an overloaded

Variadic macro trick

Deadly 提交于 2019-11-26 07:44:23
问题 What\'s the trick to create a variadic macro FOO(a1, a2, a3,..., an) such that it expands to FOOn(a1, a2, a3,..., an) for values of n in whatever preselected bounded range you choose? That is, FOO(a) should expand to FOO1(a) , FOO(a, b, c) to FOO3(a, b, c) , etc. I know there\'s a standard trick but I can\'t seem to find it. Please feel free to mark this question as a duplicate and close it if there\'s another question with the answer. I suspect there is but I couldn\'t find it. 回答1: This

MSVC++ variadic macro expansion

前提是你 提交于 2019-11-26 04:22:55
问题 So I\'ve got a macro that works nicely in GCC, but not in Microsoft\'s C++ Compiler. I\'m hoping somebody might know of a workaround, or perhaps can explain to me why it behaves this way. I\'m sure this macro isn\'t exactly \"standard\", but it would really help me out. Here is a functional example of the macro: #define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1) #define FULLY_EXPANDED(count, ...) \\ MAC ## count (__VA_ARGS__)

MSVC doesn't expand __VA_ARGS__ correctly

我与影子孤独终老i 提交于 2019-11-26 03:39:35
问题 Consider this code: #define F(x, ...) X = x and VA_ARGS = __VA_ARGS__ #define G(...) F(__VA_ARGS__) F(1, 2, 3) G(1, 2, 3) The expected output is X = 1 and VA_ARGS = 2, 3 for both macros, and that\'s what I\'m getting with GCC, however, MSVC expands this as: X = 1 and VA_ARGS = 2, 3 X = 1, 2, 3 and VA_ARGS = That is, __VA_ARGS__ is expanded as a single argument, instead of being broken down to multiple ones. Any way around this? 回答1: MSVC's preprocessor seems to behave quite differently from

Standard alternative to GCC's ##__VA_ARGS__ trick?

喜夏-厌秋 提交于 2019-11-26 03:36:42
问题 There is a well-known problem with empty args for variadic macros in C99. example: #define FOO(...) printf(__VA_ARGS__) #define BAR(fmt, ...) printf(fmt, __VA_ARGS__) FOO(\"this works fine\"); BAR(\"this breaks!\"); The use of BAR() above is indeed incorrect according to the C99 standard, since it will expand to: printf(\"this breaks!\",); Note the trailing comma - not workable. Some compilers (eg: Visual Studio 2010) will quietly get rid of that trailing comma for you. Other compilers (eg:

C++ preprocessor __VA_ARGS__ number of arguments

家住魔仙堡 提交于 2019-11-26 00:12:20
问题 Simple question for which I could not find answer on the net. In variadic argument macros, how to find the number of arguments? I am okay with boost preprocessor, if it has the solution. If it makes a difference, I am trying to convert variable number of macro arguments to boost preprocessor sequence, list, or array for further reprocessing. 回答1: This is actually compiler dependent, and not supported by any standard. Here however you have a macro implementation that does the count: #define PP