Meir
Meir

Reputation: 12755

How to use switch statements with Boost enum

I'm using the BOOST_ENUM macro and trying to write a switch statement based on a variable of the enum type I created. I get the error message that "expression must have integral or enum type"

Is there any way to use Boost enums and switch statements together?

I've seen This link, but it says to use boost::native_value, and I get the message that the Boost namespace has no native_value member. I couldn't figure out if I'm supposed to be including an extra header files for it.

Any ideas?

Example code:

BOOST_ENUM(Direction, 
(Forward)
(Backward)
)
Direction response = Direction::Forward;
switch (response)
        {
case Direction::Forward :
      return; 
    break;

Upvotes: 0

Views: 1411

Answers (1)

jleahy
jleahy

Reputation: 16855

You can't switch on a Direction object, try using switch (response.index()).

Naturally you also need to use Direction::Forward, not Action::Forward, but that may not even cause a compile error, depending how BOOST_ENUM is written.

You could also consider using a C++11 enum type:

enum class Direction { FORWARD, BACKWARD };

Upvotes: 2

Related Questions