From: David Rasmussen (pinkfloydhomer_at_[hidden])
Date: 2002-10-22 05:48:59
I was wondering if a Smart Enum library is suitable for Boost?
Enums in C++ themselves are halfbaked things. There are several things
"wrong" with them (i.e. compared with Ada etc.), of which the two worst
things, IMO, are the casting possibilities, one of which is automatic!
Sometimes an enum doesn't have a natural correspondance to some integer, so
that separation between these two types should be total, if possible. Then,
if casting one way or the other is needed, functions can be added later, and
it can be controlled whether automatic conversions should take place or not.
It's possible to make an enum-like class with these features. But a naive
approach doesn't suffice, as several good properties are wanted, that
doesn't come simple:
We want to be able to switch on our enums, somehow. Since we don't want
forced conversions to integers, we'll have to think of a smart way of doing
We want to be able to iterate over an enum, as is natural in other
languages. The ++ operator is very natural in this context. But whether it
should be done with the built-in "for" statement, or some other way, I don't
know. In many C++ idioms, the next-to-last of a sequence is very common, and
we might want to do something like that. Maybe even make it compatible with
the begin() end() of the STL. But an enum is not a container of values, it
is a value. So maybe that's not natural. In Ada, knowledge of the first or
last (or any other) element of an enum is seldom needed. Instead it has the
very elegant notion of a range. So we can do
for A in SomeEnum loop
something with A;
This can't of course be done with the built-in "for", but we could make all
sorts of these primitives for this new enum type, if possible.
If the notion of an integer and en enum can't be entirely separated in
whatever implementation we choose, we want to have optional range checking.
Another very useful feature, would be for it to have a similar interface to
a real enum. Either by making it syntactically compatible with it (probably
not possible to do with all the above requirements, because using it in a
normal switch statement for instance, would require it to automatically cast
to some publicly know type such as a real enum or an integer, which is not
desireable), or by making a common interface for smart enums and real enums
alike. For example, if we have some custom iteration mechanism (looping the
entire range) for smart enums, we should provide the same for real enums.
Similar to smart-pointer designs where we can choose to have all sorts of
checking in debug versions, and use a plain and fast pointer in release
What I've written above is just some loose ideas and some minimum
requirements of such an enum type. The exact design and the exact details I
don't know yet. But I hope that you at least recognize the need and
usefulness of such a thing. My own C++ skills are too weak to take on such a
project myself to a quality level that suits Boost, but I would still have
an opinion about the design and implementation of such a thing. I am hoping
that some skilled designers and library writers would be willing to work
with me on such a project.
Will you? :)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk