Boost logo

Boost :

Subject: Re: [boost] [C++0x] Emulation of scoped enums
From: Vicente Botet (vicente.botet_at_[hidden])
Date: 2011-02-16 13:38:48


Anthony Williams-4 wrote:
>
> Vicente Botet <vicente.botet_at_[hidden]> writes:
>
>> Daniel James wrote:
>>>
>>> 2009/3/6 vicente.botet <vicente.botet_at_[hidden]>:
>>>>
>>>> I don't think there is a better solution. You need to mask the fact
>>>> that
>>>> a enum type in C++0x is name scope and a type and can not be in C++03.
>>>
>>> A class is a name scope and a type - isn't that why it's called 'enum
>>> class'?
>>>
>>> class algae {
>>> public:
>>> enum value {green, red, cyan};
>>>
>>> algae() {}
>>> algae(value v) : v_(v) {}
>>>
>>> // And any other required methods...
>>> private:
>>> value v_;
>>> };
>>>
>>> algae x = algae::red;
>>> algae y = red; // error...
>>>
>>> This would also be closer to 'enum class' as it's strongly typed.
>>>
>>> Beman's preprocessing syntax would have to change, as the closing
>>> macro would have to know the name. The syntax using a preprocessor
>>> sequence or varargs would be the same.
>
> This is the approach I've used; I much prefer it to the namespace-based
> approach.
>
>> I've worked a little bit with the class approach and it has a main
>> portability issue.
>>
>> The use of user constructors disallows the use of the wrapper class in
>> union
>> (c++98 compilers), isn't it?
>
> Yes, but how often do you use unions? I very rarely use unions at all.
>

I don't use to use them. When I need to probide an enum for my own usage, I
think that the wrapper class will satisfy all my constraints. My concerns is
if Boost libraries can provide public enum classes in his interface with an
emulation that forbids a possible user to use it in a union, in particular
for enum classes that appear in the C++0x recomendation?

>> I think that even if we need to use a macro to name the enum class type
>> when
>> writing portable code, the namespace approach seems to have less
>> limitations.
>
> Having to use a macro whenever I mention the type name defeats the
> purpose of using scoped enums --- I might as well write algae::type if I
> have to write BOOST_SCOPED_ENUM(algae)
>

I agree that the use of the macro to make code portable is nasty, but I
don't know a better portable way. Of course, a user that is not concerned by
portability issues, could use directly algae::type or algae depending on the
compiler he is using.

Best,
Vicente

-- 
View this message in context: http://boost.2283326.n4.nabble.com/C-0x-Emulation-of-scoped-enums-tp2661312p3309467.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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