Boost logo

Boost :

From: Yuval Ronen (ronen_yuval_at_[hidden])
Date: 2005-12-13 05:14:06


Frank Laub wrote:
> Currently, there are three flavors of enumerated types available.
>
> 1. BOOST_ENUM
> This type of enum provides the user with a simple way to express an enum.
>
> example usage:
> BOOST_ENUM(boolean,
> (False)
> (True)
> )
>
> This flavor does not allow each element of the enum to be defined. It is
> assumed that the first element will begin with the value 0 and the last
> element will be equal to the count of elements - 1. The following methods
> are available with this type:
>
> static const_iterator begin()
> static const_iterator end()
> static boost::optional<const T> get(size_t index)
> static boost::optional<const T> parse(const char* str)
> const char* str() const
> bool operator == (const T& rhs) const
> bool operator != (const T& rhs) const
> ostream& operator << (ostream& os, const T& rhs)

1. What about constructors?
2. What happened to the static 'size' member?
3. The get() and parse() methods can return optional<T> rather than
optional<const T>. Since everything here is copied-by-value, the const
doesn't add much, IMO.
4. You also need operator< for set/map.
5. If you have parse() and operator<<, then I guess adding operator>>
would be logical.
6. People would probably want a value() method as you put in
BOOST_ENUM_VALUES, in here also.

> 2. BOOST_ENUM_VALUES
> This flavor of enum is the same as the basic enum flavor but it allows the
> user to specify values for each element.
>
> example usage:
> BOOST_ENUM_VALUES(VirtualKey,
> (Space)(0x20)
> (Prior)(0x21)
> (Next)(0x22)
> (End)(0x23)
> (Home)(0x24)
> )
>
> In addition to the methods described above with the basic flavor, these are
> also available:
>
> static boost::optional<const T> make(size_t value)
> size_t value() const

As far as I know, C++ allows the numeric value of a enum to be negative,
so instead of size_t (in both these functions), you need a signed type.

I also think there would be some confusion between get() and make().
With BOOST_ENUM, get() is naturally the only choice. So users would be
tempted to use get() also with BOOST_ENUM_VALUES. But this is probably
not what they are after. With BOOST_ENUM_VALUES, make() is what is most
useful, and IMO, get() is mostly useless. So maybe get() should do what
make() does today, and the old get() functionality should be names
get_by_index() or something similar.

> As always, any comments are much appreciated.

All in all, great job!
Yuval


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