C++ preprocessor __VA_ARGS__ number of arguments

后端 未结 12 2120
一向
一向 2020-11-22 08:03

Simple question for which I could not find answer on the net. In variadic argument macros, how to find the number of arguments? I am okay with boost preprocessor, if it has

12条回答
  •  囚心锁ツ
    2020-11-22 08:40

    There are some C++11 solutions for finding the number of arguments at compile-time, but I'm surprised to see that no one has suggested anything so simple as:

    #define VA_COUNT(...) detail::va_count(__VA_ARGS__)
    
    namespace detail
    {
        template
        constexpr std::size_t va_count(Args&&...) { return sizeof...(Args); }
    }
    

    This doesn't require inclusion of the header either.

提交回复
热议问题