Boost logo

Boost :

From: Dave Harris (brangdon_at_[hidden])
Date: 2003-05-26 12:53:48


Would there be any interest in adding specific support for enums to
operators.hpp? Enums are used in two main idiomatic ways - as bit-flags or
as sequential cases - and it would be good to have direct and explicit
support for these. However, enums are not class types and cannot use the
inheritance mechanism which the current operators.hpp relies on.

What I have in mind is a syntax like:

    namespace Colours {
        using namespace boost::enum_types::ordinal;
        enum Type {
            red, blue, green, end
        };
    }
    
    namespace IoMode {
        using namespace boost::enum_types::bitflag;
        enum Type {
            read=0x01, write=0x02, readwrite=0x03
        };
    }
    
    for (Colours::Type i(0); i != Colours::end; ++i)
        use( i );
    
    IoMode mode = IoMode::read | IoMode::write;

    Colours g = red | blue; // Error.
    ++mode; // Error.

The key points here are that each enum has its own namespace, and that a
using-declaration pulls the appropriate operators into that namespace. In
my view such a namespace is a good idea anyway, to avoid name clashes
(especially with names like "end" which I use for one-past-the-end
values). If you disagree then you probably won't be interested in this
proposal. Is there a better way to solve the problem?

I don't yet have an implementation, and I've not submitted code to boost
before. One difficulty for me is that this may need argument-dependent
look-up, which my compiler doesn't support.

-- Dave Harris


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