Re: [boost] Is there interest in a library for string-convertible enums?
Artyom Tokmakov wrote:
In addition to various useful suggestions on the list, I would ask for implementation to support (almost) any large number of enum elements (enumerators) within enum declaration.:
I have ad-hoc implementation of my own which uses Boost.Proprocessor's sequence to define enum elements. Then I can generate whatever parser/printer/other code I want (I find that very useful).
There's a disadvantage, however: obvious limitation of max 256 elements (at least, by default) coming from Boost.Preprocessor.
And that’s where the problem lies. To enable large numbers of enum values, I’d have to find a solution without using Boost.PP. Without C++ introducing argument number based macro overloading, that is not possible, short of defining my own macro sequences for more than 256 elements, which I would like to avoid. I am still thankful that you asked for this, because it uncovered a huge implementation problem which induces enormous compilation overhead (1 minute for 48 enum values on my laptop). I will do my best to make the library comprehend large enums, but I can’t guarantee anything larger than 256. --- Felix Uhl
And that’s where the problem lies. To enable large numbers of enum values, I’d have to find a solution without using Boost.PP. Without C++ introducing argument number based macro overloading, that is not possible, short of defining my own macro sequences for more than 256 elements, which I would like to avoid.
I wonder, how many people are out there who want to go beyond 256 :) Have you considered X-macro trick (say, here: http://stackoverflow.com/a/650729/1198287)? That definitely doesn't have those limitations, looks simple enough (although not as neat as Boost.Preprocessor), and also allows you to generate many things out of it. Just now tested it with 500 enum elements: #include <iostream> #define MYENUM \ X(Val1) \ X(Val2) \ ... X(Val499) \ X(Val500) enum class MyEnum { #define X(EnumVal_) EnumVal_, MYENUM #undef X }; const char* ToString(MyEnum e) { switch (e) { #define X(EnumVal_) case MyEnum::EnumVal_: return #EnumVal_; MYENUM #undef X default: return "Unknown!"; } } int main() { std::cout << ToString(MyEnum::Val102) << std::endl; std::cout << ToString(MyEnum::Val500) << std::endl; } -- Best regards, Artem Tokmakov
participants (2)
-
Artyom Tokmakov
-
Felix Uhl