Boost logo

Boost Users :

Subject: Re: [Boost-users] Preprocessor Question
From: Roman Perepelitsa (roman.perepelitsa_at_[hidden])
Date: 2009-11-16 06:32:24


2009/11/16 Surya Kiran Gullapalli <suryakiran.gullapalli_at_[hidden]>

> 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/77ee8c8f92e4a3fb/346fc464319b1ee5

Roman Perepelitsa.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net