The BOOST_PP_VA_OPT variadic macro is a more flexible alternative to the C++20 __VA_OPT__ construct. It expands to either one of two inputs depending on whether the variadic data is empty or not, whereas the C++20 __VA_OPT__ constructs expands to either its input or nothing depending on whether the variadic data is empty or not. This macro only exists when the compilation is at the C++20 level and the __VA_OPT__ construct is supported.

#### Usage

BOOST_PP_VA_OPT(x,y,...) (v)

#### Arguments

x
A tuple whose data is the macro expansion if the variadic data is not empty
y
A tuple whose data is the macro expansion if the variadic data is empty
,,,
The variadic data to be checked for emptiness

#### Remarks

When the macro invocation BOOST_PP_VARIADIC_HAS_OPT() expands to 1, then this macro exists and can be invoked, otherwise this macro does not exist and attempting to invoke it will lead to a preprocessor error that the macro can not be found. Because of this condition the header file for including this macro includes the header file for the BOOST_PP_VARIADIC_HAS_OPT macro.
The difference between this macro and the __VA_OPT__ construct illustrates a limitation of the latter construct with a trade off of simpler syntax. The differences between the __VA_OPT__ construct and this macro are:

- The __VA_OPT__ construct offers a choice as its expansion only between its input preprocessing tokens or nothing ( called a "single placemarker token" ) depending on whether the implicit variadic data is empty or not. There is no way using the __VA_OPT__ construct to specify any alternative but the "single placemarker token" when the variadic data is empty whereas any preprocessing tokens can be specified when the variadic data is not empty. With the BOOST_PP_VA_OPT macro the user can specify as its expansion preprocessing tokens both when the variadic data is empty and when the variadic data is not empty.
- The __VA_OPT__ construct offers a simple syntax whereas this macro is more verbose. The BOOST_PP_VA_OPT macro's first and second parameters must be Boost PP tuples of data, in order to expand to normal or variadic data, and the third parameter must be the variadic data to check for emptiness, whereas the __VA_OPT__ construct has an implied variadic data as __VA_ARGS__ to check for emptiness and can specify its expansion directly in terms of its input.
- The __VA_OPT__ construct can only be specified in the replacement list of some macro, whereas the BOOST_PP_VA_OPT macro can be used both as an alternative to the __VA_OPT__ construct in the replacement list of some macro and anywhere else a macro can be used.
- It is impossible to have a left parenthesis '(' or a right parenthesis ')' as preprocessing token data within the __VA_OPT__ construct whereas both are possible as part of the expanded data for the BOOST_PP_VA_OPT macro.

The exact BOOST_PP_VA_OPT equivalent to the construct of '__VA_OPT__ ( pp-tokens )' in the replacement list of a macro is 'BOOST_PP_VA_OPT (( pp-tokens ),(),__VA_ARGS__)'.

#### See Also

- BOOST_PP_VARIADIC_HAS_OPT

#### Requirements

Header: <boost/preprocessor/facilities/va_opt.hpp>

#### Sample Code

    #include <boost/preprocessor/facilities/va_opt.hpp>

    # if BOOST_PP_VARIADIC_HAS_OPT()

    #define DATA
    #define OBJECT OBJECT2
    #define OBJECT2
    #define FUNC(x) FUNC2(x)
    #define FUNC2(x)
    #define FUNC_GEN(x,y) (1,2,3)

    BOOST_PP_VA_OPT((1),(2),DATA)                      // expands to 2
    BOOST_PP_VA_OPT((3),(4),OBJECT)                    // expands to 4
    BOOST_PP_VA_OPT((5),(6),FUNC(1))                   // expands to 6
    BOOST_PP_VA_OPT((7,8),(9,10),FUNC)                 // expands to 7,8
    BOOST_PP_VA_OPT((1,2,3,4,5),(6,7,8,9,10),FUNC_GEN) // expands to 1,2,3,4,5

    #endif

© Copyright Edward Diener 2019

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt)