Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2003-05-28 10:54:35

John Torjo wrote
> > I have adopted a Java-style approach to enumerated-style values (don't
> > if this is the best approach, just the way that I do it now). For

> > class WhatIsTheMatrix
> > {
> > public:
> > typedef unsigned char type;
> > public:
> > static const type TheWhat = 0;
> > static const type Matrix = 1;
> > static const type AniMatrix = 2;
> > static const type MatrixReloaded = 3;
> > static const type MatrixRevolutions = 4;
> > };

>I think this is really cool!


>Basically, you should be able to do
>WhatIsTheMatrix::type m = WhatIsTheMatrix::TheWhat;
>switch( m) {
>case WhatIsTheMatrix::TheWhat: //...
>case WhatIsTheMatrix::AniMatrix: //...

>The unfortunate thing is that you lose compile-time checking. You'll be
>to say something like
>WhatIsTheMatrix::type m = 20;

This could be resolved using something like the ideas by Dave Harris and
Daniel Frey. This would then make WhatIsTheMatrix the enumerated type, thus
allowing you to do operator overloading on the class.

The class could then be defined in terms of something like:
   boost::enumeration::range< type, lower_value, upper_value >
to restrict the values within a given range. Thus:

WhatIsTheMatrix m = WhatIsTheMatrix::TheWhat;
switch( m )
   case WhatIsTheMatrix::TheWhat: //...
   case WhatIsTheMatrix::AniMatrix: //...

The bitflag enumeration usage would have the form:
   boost::enumeration::bitflags< type, mask >
where mask will be used to check if a flag is valid or not.

>What I would really love is some library to allow me to do something
>to this:


This would be a combined range-bitflag enumerated type:
   boost::enumeration::range_bitflag< type, lower_value, upper_value, mask >


>I'm not sure operators like ++,--, +,-, etc. should be made workable for

I agree. An enumerated type should have the form:

template< typename EnumType > class enumerated
      typedef EnumType enum_type;
      inline enumerated & operator=( const enumerated & );
      inline enumerated & operator=( enum_type ); // may throw if invalid
      inline enumerated(); // some default value
      inline enumerated( enum_type ); // may throw if invalid value
      inline enumerated( const enumerated & );

with == and != defined for
   ( enumerated, enumerated )
   ( enumerated, enumerated::enum_type )
   ( enumerated::enum_type, enumerated )

I have said that assignment or construction to an invalid enum_type will
throw an exception, because although this means that checking occurs at
run-time (harder to debug), it is easier to implement.

NOTE: These are just suggestions and may not be the best way to go about


It's fast, it's easy and it's free. Get MSN Messenger today!

Boost list run by bdawes at, gregod at, cpdaniel at, john at