
Hello all, I'm trying to come up with a solution for the following problem. I've a macro with three arguments, but if the third is not given, it should be assumed as '0'. Something on the lines of default arguments for functions. I'm using VS2005, and g++ 4.4.x so i can use Variadic macros, but I'm unable to come up with a solution. Is there any way i can achieve this using Boost.Preprocessor ? I tried using BOOST_PP_IF but unable to proceed further. Any Ideas ? Surya

2009/11/16 Surya Kiran Gullapalli
Hello all, I'm trying to come up with a solution for the following problem.
I've a macro with three arguments, but if the third is not given, it should be assumed as '0'. Something on the lines of default arguments for functions.
I'm using VS2005, and g++ 4.4.x so i can use Variadic macros, but I'm unable to come up with a solution. Is there any way i can achieve this using Boost.Preprocessor ?
I tried using BOOST_PP_IF but unable to proceed further. Any Ideas ?
#define FOO_IMPL(a, b, c) /* Definition of your macro. 3 arguments. */ // Expands to FOO_IMPL(a, b, c) if called with 3 arguments. // Expands to FOO_IMPL(a, b, 0) if called with 2 arguments. // En error if called with some other number of arguments. #define FOO(...) \ BOOST_PP_IF( \ BOOST_PP_EQUAL(PP_NARG(__VA_ARGS__), 2), \ FOO_IMPL(__VA_ARGS__, 0), \ FOO_IMPL(__VA_ARGS__)) Definition of PP_NARG is here: http://groups.google.com/group/comp.std.c/browse_thread/thread/77ee8c8f92e4a... Roman Perepelitsa.

Definition of PP_NARG is here: http://groups.google.com/group/comp.std.c/browse_thread/thread/77ee8c8f92e4a...
This does not seems to be working, I keep getting the error C4003, not enough actual parameters for macro 'PP_ARG_N' GCC says, PP_ARG_N expects 65 args, where as it is getting only 3. I'm kinda lost here . Surya

2009/11/16 Surya Kiran Gullapalli
Definition of PP_NARG is here:
http://groups.google.com/group/comp.std.c/browse_thread/thread/77ee8c8f92e4a...
This does not seems to be working, I keep getting the error C4003, not enough actual parameters for macro 'PP_ARG_N'
GCC says, PP_ARG_N expects 65 args, where as it is getting only 3. I'm kinda lost here .
Hm... that's because both branches of BOOST_PP_IF always get evaluated. We need to evaluate only one of them. #define FOO_ARGS(...) \ BOOST_PP_IF( \ BOOST_PP_EQUAL(PP_NARG(__VA_ARGS__), 2), \ (__VA_ARGS__, 0), \ (__VA_ARGS__)) #define FOO(...) BOOST_PP_EXPAND(FOO_IMPL FOO_ARGS(__VA_ARGS__)) I'm attaching a complete solution. Roman Perepelitsa.

Thanks for the solution, but it was actually failing in getting number of arguments to the macro. warning C4003: not enough actual parameters for macro 'PP_ARG_N' so PP_NARG evaluates to nothing and subsequently everything fails. Thanks, Surya

Thanks for the solution,
but it was actually failing in getting number of arguments to the macro.
warning C4003: not enough actual parameters for macro 'PP_ARG_N'
so PP_NARG evaluates to nothing and subsequently everything fails.
Please note that the VC preprocessor does not support variadic macros, which makes the valid implementation of PP_NARG() fail. Regards Hartmut ------------------- Meet me at BoostCon http://boostcon.com

AMDG Hartmut Kaiser wrote:
Thanks for the solution,
but it was actually failing in getting number of arguments to the macro.
warning C4003: not enough actual parameters for macro 'PP_ARG_N'
so PP_NARG evaluates to nothing and subsequently everything fails.
Please note that the VC preprocessor does not support variadic macros, which makes the valid implementation of PP_NARG() fail.
I can compile void f(int, int) {} #define test(...) __VA_ARGS__ int main() { f(test(57, 21)); } with msvc 9.0. In Christ, Steven Watanabe

Hartmut Kaiser wrote:
Thanks for the solution,
but it was actually failing in getting number of arguments to the macro.
warning C4003: not enough actual parameters for macro 'PP_ARG_N'
so PP_NARG evaluates to nothing and subsequently everything fails.
Please note that the VC preprocessor does not support variadic macros, which makes the valid implementation of PP_NARG() fail.
I can compile
void f(int, int) {}
#define test(...) __VA_ARGS__ int main() { f(test(57, 21)); }
with msvc 9.0.
I stand corrected! Thanks for the info. Regards Hartmut ------------------- Meet me at BoostCon http://boostcon.com

Well, MS says variadic macros are supported from VS2005. I've tested other variadic macros and they're working fine, except this one. I've tested this on gcc as well with similar results. The problem seems to be in substitution of PP_RSEQ_N() macro in PP_NARG. If I write all the values from 63 to 0 instead of PP_RSEQ_N, then it will compile fine, but then the number of arguments always evaluates to 1. I've tested this with VS2005 and VS2008.

2009/11/16 Surya Kiran Gullapalli
Well, MS says variadic macros are supported from VS2005.
I've tested other variadic macros and they're working fine, except this one. I've tested this on gcc as well with similar results.
Did you actually try to compile the program I attached? It works fine with gcc. Roman Perepelitsa.

It seems to be a bug with msvc8 and msvc9 Details here. http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?Feedbac... Thanks, Surya
participants (4)
-
Hartmut Kaiser
-
Roman Perepelitsa
-
Steven Watanabe
-
Surya Kiran Gullapalli