c++ - Getting size of enum using preprocessor -


i trying find way calculate length of enum, other adding "count" element @ end of enum. have found way use preprocessor follows.

#include <iostream> #include <boost/preprocessor/tuple/elem.hpp>  //simple declaration template <class e> struct enum_size;  //specialization done in macro each enum created #define make_enum(name, ...) enum name {__va_args__};   \   template <>                       \   struct enum_size<name> {                  \     static const int value = boost_pp_variadic_size(__va_args__);   \     };  make_enum(my_enum1, a, b, c) //make_enum(my_enum2, a) //triggers compilation error  int main(int argc, char** argv) {   std::cout << enum_size<my_enum1>::value << std::endl; } 

however when try create my_enum2 above redeclaration error compiler (gcc 4.8.3 on cygwin) follows

main.cpp:16:21: error: redeclaration of 'a'  make_enum(my_enum2, a)                  ^ main.cpp:9:41: note: in definition of macro 'make_enum'  #define make_enum(name, ...) enum name {__va_args__}; \                                      ^ main.cpp:15:21: note: previous declaration 'my_enum1 a'  make_enum(my_enum1, a, b, c)                  ^ main.cpp:9:41: note: in definition of macro 'make_enum'  #define make_enum(name, ...) enum name {__va_args__}; \ 

if change problematic line make_enum(my_enum2, e) compiles cleanly. ideas wrong here , how can fix it? in advance!

variable a has type my_enum1, cannot redeclare it. can fix few ways

1) if have c++11 use enum class, instead of enum.

#define make_enum(name, ...) enum class name {__va_args__}; 

2) can put enum namespace.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -