Boost logo

Boost :

Subject: Re: [boost] [C++0x] Emulation of scoped enums
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2009-03-06 17:07:58


On Fri, Mar 6, 2009 at 1:08 PM, Andrey Semashev
<andrey.semashev_at_[hidden]>wrote:

> Could this be rewritten to use structs instead of namespaces? This would
> allow to declare enums nested in classes.
>

Agreed. In retrospect, this is pretty important now that it's been noticed.

On Fri, Mar 6, 2009 at 6:39 AM, Daniel James <daniel_james_at_[hidden]>
 wrote:

> 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_;
> };

I worry about an implementation like this because it opens up a bunch of
very subtle difference that could otherwise be avoided. For instance,
perhaps the simples problem is that now decltype( algae::green ) is not
equal to the "enum" type that is intended to be used (rather it is an actual
enum nested in that type).

One quick example of how this can show up as a problem in seemingly trivial
code is operator overloading. Imagine a very simple color enum with
enumerations "red = 1", "green = 2", "blue = 4", and let's say you decide to
overload operator | to combine colors. If you write the overload using the
type "color"... as long as at least one of your operands is not one of the
enumerated constants named directly. For instance, color( red ) | color(
blue ) works fine, however red | blue would not because the operator was
overloaded for the encapsulating type as opposed to the nested value type!

Another problem is that the type can no longer be referenced as a recognized
enum type to the compiler, so you can't, for instance, use it directly as
the type of a non-type template parameter (instead you would have to use,
continuing the example above, color::value, which wouldn't work for
compilers that actually support scoped enums).

In the interest of creating the least amount of subtle differences, I think
leaving the type as an actual enum type is going to be the best solution.

-- 
-Matt Calabrese

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