Boost logo

Boost :

Subject: Re: [boost] Scoped Enum Emulation
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2012-01-24 17:17:15


Le 24/01/12 22:51, Beman Dawes a écrit :
> On Tue, Jan 24, 2012 at 12:06 PM, Joshua Boyce
> <raptorfactor_at_[hidden]> wrote:
>> I'm currently updating some of my code to take advantage of C++0x features
>> where available, but I also want to retain backwards compatibility with
>> C++03. I was looking at scoped enums and noticed a thread on the mailing
>> list by Beman Dawes discussing a scoped enum emulation implementation for
>> Boost. I see that it's currently being used by Boost, and resides in
>> /boost/detail/scoped_enum_emulation.hpp.
>>
>> My question is, why is this an implementation detail? Would it be possible
>> for it to be moved to Boost.Config with the other 'helper' macros
>> like BOOST_CONSTEXPR?
>>
>> Obviously it's not hard to reimplement, but I figured that it would be
>> useful to other library users, so it would be best (imo) if it were moved
>> somewhere public, rather than being hidden away.
> It isn't a full emulation of C++11's scoped enum feature. That's
> probably why it ended up in detail rather than config, although I've
> forgotten the detailed rationale, if there ever was any.
>
> What do others think? Should /boost/detail/scoped_enum_emulation.hpp
> functionality be moved to config?
>
> Are there any improvements that would make the emulation better,
> without turning something simple into something complex?
>
>

Hi,

I'm working with a different emulation which uses classes. For example

   // enum class cv_status;
   BOOST_DECLARE_STRONG_ENUM_BEGIN(cv_status)
   {
     no_timeout,
     timeout
   };
   BOOST_DECLARE_STRONG_ENUM_END(cv_status)

The macros are defined as follows:

#ifdef BOOST_NO_SCOPED_ENUMS
#define BOOST_DECLARE_STRONG_ENUM_BEGIN(x) \
   struct x { \
     enum enum_type

#define BOOST_DECLARE_STRONG_ENUM_END(x) \
     enum_type v_; \
     inline x() {} \
     inline x(enum_type v) : v_(v) {} \
     inline operator int() const {return v_;} \
     friend inline bool operator ==(x lhs, int rhs) {return lhs.v_==rhs;} \
     friend inline bool operator ==(int lhs, x rhs) {return lhs==rhs.v_;} \
     friend inline bool operator !=(x lhs, int rhs) {return lhs.v_!=rhs;} \
     friend inline bool operator !=(int lhs, x rhs) {return lhs!=rhs.v_;} \
   };

#define BOOST_STRONG_ENUM_NATIVE(x) x::enum_type
#else // BOOST_NO_SCOPED_ENUMS
#define BOOST_DECLARE_STRONG_ENUM_BEGIN(x) enum class x
#define BOOST_DECLARE_STRONG_ENUM_END(x)
#define BOOST_STRONG_ENUM_NATIVE(x) x
#endif // BOOST_NO_SCOPED_ENUMS

While this is not yet a complete emulation of scoped enums, it has the
advantage of that there is no need to use a macro to name the strong type.

I don't know if it will be confusing to put provide both approaches in a
single mini library.

Best,
Vicente


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk