Boost logo

Boost :

Subject: Re: [boost] [C++0x] Emulation of scoped enums
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2009-03-05 12:15:33


Beman Dawes wrote:
> The improved type safety of the C++0x scoped enum feature is attractive,
> so I'd like to start using it as it becomes available in compilers.
>
> See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf
> for a description. Note that the committee changed the name from
> strongly typed enums to scoped enums.
>
> See the attached for a boost/detail header that provides macro wrappers
> that use scoped enums if available, otherwise fall back on C++03
> namespaces and unscoped enums. It lets a library write:
>
> BOOST_SCOPED_ENUM_START(algae) { green, red, cyan };
> BOOST_SCOPED_ENUM_END
> ...
> BOOST_SCOPED_ENUM(algae) sample( algae::red );
> void func(BOOST_SCOPED_ENUM(algae));
>
> and then a user can write:
>
> sample = algae::green;
> func( algae::cyan );

Good plan. Thoughts:

- No need to make it a ::detail!

- The user needs to spell out the macro if they declare a variable; can
this be avoided?

- How about avoiding the _START/_END stuff with e.g.
   BOOST_SCOPED_ENUM_DECL(algae, green,red,cyan);
   (I had been under the impression that varargs macros were a gcc-ism,
but I've recently discovered that they're in c99; do non-gcc C++
compilers generally allow them? I don't think we can get away with
allowing only up-to-N elements in this case.)

- I agree with Kjell about the double negative :-)

Phil.


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