enum class

Discussion in 'C++' started by Chris Forone, Dec 17, 2012.

  1. Chris Forone

    Chris Forone Guest

    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
    Chris Forone, Dec 17, 2012
    #1
    1. Advertising

  2. On 17.12.2012 11:56, Chris Forone wrote:
    > 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
    Marcel Müller, Dec 17, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    519
  2. Jerminia
    Replies:
    3
    Views:
    608
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    436
    Rolf Magnus
    Nov 13, 2003
  4. Brian
    Replies:
    4
    Views:
    2,573
    Brian
    Feb 27, 2010
  5. Ansel

    "enum" vs. "enum class"

    Ansel, Aug 26, 2012, in forum: C++
    Replies:
    16
    Views:
    730
    Ansel
    Aug 27, 2012
Loading...

Share This Page