enum class

C

Chris Forone

is it possible to use enum class as index in std::vector or
std::valarray without explicit conversation? ms vs express 2012 do not
compile something like:

enum class Coordinate : std::size_t { X, Y, Z };

std::valarray<float> position(3);
position[Coordinate::X] = 1.0f;

old style (without class) does!

thanks & greetings, chris
 
M

Marcel Müller

is it possible to use enum class as index in std::vector or
std::valarray without explicit conversation?

AFAIK there is no implicit conversion from enum class to int.

But you can write your own vector, deriving from std::vector (protected)
and exposing only the functions you like to keep while overwriting
others with strongly typed version using your enum as index.

enum class Coordinate : std::size_t { X, Y, Z };

std::valarray<float> position(3);
position[Coordinate::X] = 1.0f;

old style (without class) does!

Yes because there is an implicit conversion to int.

But you can still use old style enums, if you want to keep the
compatibility to not strongly typed methods.

class Coordinate
{ enum { X, Y, Z };
};


Marcel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,245
Members
46,839
Latest member
MartinaBur

Latest Threads

Top