|
Boost : |
From: Rob Stewart (stewart_at_[hidden])
Date: 2004-09-13 11:38:20
From: "Noah" <noah_at_[hidden]>
>
> I hate having the shift value represent the flag - it's too easy to believe
> that you're just supposed to pass the value as is. Then you have to go
> lookup whether the value is a shift or the flag value itself. It's a
> serious pain.
I don't understand what you mean. I've never been confused when
using left shift to generate the enumerator-definition.
> You could have the best of both worlds if you wrap the parameter up in a
> class. Here's a brief sketch off the top of my head (so don't try to
> compile and use it!):
>
> template<typename E>
> class BitFlag
> {
> int flag;
>
> public:
> BitFlag(E val)
> { flag = 1 << val; }
> };
>
> It makes the enum much easier to deal with since you can just simply put the
> flags in a list and not worry about explicitly initializing each flag. You
> can also add additional functionality. You could overload operators to
> handle passing or masking multiple flags. You could also, through the use
> of a traits class, validate flag combinations.
However wonderful that could be, the fact is that an
enumerator-definition must be a constant-expression.
-- Rob Stewart stewart_at_[hidden] Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk