Boost logo

Boost :

Subject: Re: [boost] Scoped Enum Emulation
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2012-01-26 14:34:48


Vicente J. Botet wrote:
> Le 26/01/12 14:56, Stewart, Robert a écrit :
>
> >> inline NT(enum_type v) : v_(v) {} \
> >> inline underlying_type underlying() const {return
> v_;} \
> >
> > I've always preferred naming such functions with an "as_"
> > prefix. In this context, you actually know the name of the
> > underlying type, so you could even do as_##UT, though that
> > wouldn't work right for unsigned int, signed int, etc. Maybe
> > the preprocessor can help, for example, to collapse "unsigned
> > int" to "unsigned" so you could generate "as_unsigned" even
> > when UT is "unsigned int".
>
> I have no used underlying yet. It was just there to show a
> workaround when there is no explicit conversion. Anyway to make
> the user code portable, he can not use the member function as
> enum class don't have it. That mean s that we need a non-member
> function, let me call it underlying_cast,
>
> namespace boost
> {
> #ifdef BOOST_NO_SCOPED_ENUMS
> template <typename UT, typename NT>
> UT underlying_cast(NT v)
> {
> return v.underlying();
> }
> #else // BOOST_NO_SCOPED_ENUMS
> template <typename UT, typename NT>
> UT underlying_cast(NT v)
> {
> return static_cast<UT>(v);
> }
> #endif
> }
>
> The portable explicit conversion to the underlying type
> becomes
>
> int i = boost::underlying_cast<int>(e);

That is better. The existence of underlying() is, then, an implementation detail. Indeed, you might name it something more ominous: "detail_as_underlying", perhaps.

> At the end I'm wondering if the implicit conversion should
> even be provided

Without it, static_cast<underlying_type>(e) won't work, but that's valid for the real thing.

> I will commit something on the sandbox soon so that we can
> continue improving it.

OK

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP http://www.sig.com

________________________________

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


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