How to count the number of macros transferred to a variable macro?

I already love most:

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

Doing this with the -E flag in GCC displays the following

3 2 1 1

When do I need:

3 2 1 0

What am I doing wrong here? I am not configured to use boost preprocessor , but I need the solution to be variable.

Any ideas how to make this work?

+7
c ++ boost-preprocessor
source share
1 answer

With COUNT() , you have one empty argument.

You can 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, a variation pattern may be a solution.

 template <typename ... Ts> constexpr std::size_t Count(Ts&&...) { return sizeof...(Ts); } 
+6
source share

All Articles