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 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

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)