9

I'm already most of the way there:

#include <boost/preprocessor.hpp>
#define COUNT(...) BOOST_PP_VARIADIC_SIZE(__VA_ARGS__)
COUNT(1,2,3)
COUNT(1,2)
COUNT(1)
COUNT()

Running this with -E flag in GCC outputs the following

3 2 1 1

When what I need is:

3 2 1 0

What am I doing wrong here? I'm not set on using boost preprocessor, but I do need the solution to be variadic.

Any ideas how to get this to work?

quant
  • 21,507
  • 32
  • 115
  • 211
  • 1
    @JoachimPileborg I'm running `g++` with the `-E` flag to expand the macro. It *is* verifiable. – quant Aug 08 '14 at 08:27
  • 3
    `COUNT()` *is* passing one argument; it's just empty. – Simple Aug 08 '14 at 09:13
  • I've not used Boost.Preprocessor much, so this might be too complex, but does `BOOST_PP_IF(BOOST_PP_GREATER(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), 1), BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), BOOST_PP_NOT(BOOST_PP_IS_EMPTY(__VA_ARGS__)))` work? – Simple Aug 08 '14 at 09:24

1 Answers1

7

With COUNT(), you have one empty argument.

You may use something like:

#define PP_IS_EMPTY(...) (#__VA_ARGS__[0] == '\0' ? 1 : 0)
#define PP_COUNT(...) ((!PP_IS_EMPTY(__VA_ARGS__)) * (BOOST_PP_VARIADIC_SIZE(__VA_ARGS__)))

Alternatively, variadic template may be a solution.

template <typename ... Ts>
constexpr std::size_t Count(Ts&&...) { return sizeof...(Ts); }
Jarod42
  • 203,559
  • 14
  • 181
  • 302