[enums] Interest in an alternative emulation of scoped enums classes

Hi, I have been working in an alternative implementation of the Beman's Scoped enums macros (http://boost.2283326.n4.nabble.com/template/NamlServlet.jtp?macro=new_topic&node=2600599) Daniel James proposed. The macros are very similar BOOST_ENUM_CLASS_START(algae, uint8) { green, red, cyan } BOOST_ENUM_CLASS_END(algae, uint8) algae sample( algae::red ); void func(algae); The major advantages are: * No need to use a macro to name the type in most of the cases * The type is not implicitly convertible to the underlying type neither to bool and so it is closer the intended goal of strongly enums. * Allows to state explicitly the underlying type The major liabilities are: * In some context we can not use the enum class. This is the case of switch statements and template parameters. In order to get the native enum value a free function enums::get_value allows to workaround that in a portable way. algae v; ... switch (boost::enums::get_value(v)) { case algae ::green: return("EnumClass::Default"); case algae ::red: return("EnumClass::Enum1"); case algae ::cyan : } * the emulation is not an enum, that is is_enum(algae)::value is false The user can use is_enum<enum_type<EC>::type> or the library could provide an is_enum nested on the enums namespace. enums::is_enum<EC> or we can specialize boost::is_enum<EC> For template parameters the meta-function enum_type gives the type of the native enum used by the emulation. template <enums::enum_type<algae>::type V> struct ex; * Use in union The emulation providing constructors (which is optional) can not be used in unions if the compiler doesn't allows class with constructors in unions. * default value There is no portable way to manage with standard *implicit* default values if the enum class emulation can not define constructors. I have found an explicit way that allows to assign the default value. Use default_value<EC>(). This function will be equivalent to EC() if enum class is supported by the compiler. algae e(enums::default_value<algae>()); * Explicit conversion from int There is no portable way to manage with standard *explicit* conversion of ints if the enum class emulation can not define constructors. I have found an explicit way that allows to manage with explicit conversion using a function convert_to<EC>(v) instead of the Constructor. This function will be equivalent to EC(v) if enum class is supported algae e(convert_to<algae>(1)); There is not too much documentation yet. You can find the code and some test in the sandbox https://svn.boost.org/svn/boost/sandbox/enums I could provide alternative macros for users considering a better approach necessary * PP Sequence Syntax BOOST_ENUM_CLASS(EnumClass, Underlying, ( (Enum0) (0) ) ( (Enum1) ) ( (Enum2) ) ); PP Variadic Syntax BOOST_ENUM_CLASS(EnumClass, Underlying, (Enum0) (0), Enum1, Enum2 ); With the macros the meta-functions and the functions we can write portable code using strongly scoped enums. Is there any interest for this in Boost? Comments or suggestions appreciated, Vicente P.S. the convert_to function is the one of Boost.Conversion. -- View this message in context: http://boost.2283326.n4.nabble.com/enums-Interest-in-an-alternative-emulatio... Sent from the Boost - Dev mailing list archive at Nabble.com.

Hi Vicente,
BOOST_ENUM_CLASS_START(algae, uint8) { green, red, cyan } BOOST_ENUM_CLASS_END(algae, uint8)
algae sample( algae::red ); void func(algae);
does your lib return a textual representation for the enum, like: algae ag( algae::green); std::cout << ag.str() << "\n"; // prints out 'green' Oliver -- Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

Oliver Kowalke wrote:
Hi Vicente,
BOOST_ENUM_CLASS_START(algae, uint8) { green, red, cyan } BOOST_ENUM_CLASS_END(algae, uint8)
algae sample( algae::red ); void func(algae);
does your lib return a textual representation for the enum, like:
algae ag( algae::green); std::cout << ag.str() << "\n"; // prints out 'green'
Hi Oliver, No, it doesn't and I can not do it with the current macros. With the PP-SEQ and the variadic macros I could generate something allowing print a textual representation for the enum as the Enum library in the Vault does, std::cout << c_str(ae) << "\n"; // could prints out 'green' but I'm sure this will be begin of long list of requests, locales adaptations, input from a string representation, ... I would prefer to concentrate on a pure scoped enum classes emulation for the first release of the library. Best, Vicente -- View this message in context: http://boost.2283326.n4.nabble.com/enums-Interest-in-an-alternative-emulatio... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Sun, Feb 27, 2011 at 3:45 PM, Vicente Botet <vicente.botet@wanadoo.fr> wrote:
The major liabilities are:
Does it support bitfields? -- Dave Abrahams BoostPro Computing http://www.boostpro.com

Dave Abrahams wrote:
On Sun, Feb 27, 2011 at 3:45 PM, Vicente Botet <vicente.botet@wanadoo.fr> wrote:
The major liabilities are:
Does it support bitfields?
I guess I don't understand the question. If you are you requesting if the emulation can be used as the type of a bitfield, the response is not, so yes I will need to add it in the liabilities list :( Best, Vicente -- View this message in context: http://boost.2283326.n4.nabble.com/enums-Interest-in-an-alternative-emulatio... Sent from the Boost - Dev mailing list archive at Nabble.com.

On Tue, Mar 1, 2011 at 12:09 PM, Vicente Botet <vicente.botet@wanadoo.fr> wrote:
I guess I don't understand the question.
I guess you do :-) -- Dave Abrahams BoostPro Computing http://www.boostpro.com

Dave Abrahams wrote:
On Tue, Mar 1, 2011 at 12:09 PM, Vicente Botet <vicente.botet@wanadoo.fr> wrote:
I guess I don't understand the question.
I guess you do :-)
Thinking about it a little more, we could try to use these enums as types of the portable emulation of bitfields with the Boost.Bitfield library. If it works, the user could always have a portable implementation of bitfields and scoped enums and mix them as needed. Thanks for making me think on this, Vicente -- View this message in context: http://boost.2283326.n4.nabble.com/enums-Interest-in-an-alternative-emulatio... Sent from the Boost - Dev mailing list archive at Nabble.com.
participants (3)
-
Dave Abrahams
-
Oliver Kowalke
-
Vicente Botet