2

I have a templated struct with some enumerations in it and I'd like to make a std::array with the enumerations in it for convenience. Is there any way of doing the following?

template< typename A >
struct someClass{
    enum class State{
        sA,
        sB,
        sC
    }

    static const std::array<State,4> the_states = {{
        State::sA,
        State::sB,
        State::sC
    }};

};
5
  • In C++03? or C++11? enum class and std::array<> are available with C++11 only. So I suppose your question needs proper tags. Commented Oct 15, 2011 at 18:49
  • Why are you encapsulating an enum class and then further "abstracting" it through an explicit array? Commented Oct 15, 2011 at 18:49
  • The idea is just for convenience of iterating over the states. Commented Oct 15, 2011 at 18:50
  • shuttle87: then make the enum's underlying type explicit (you're already using strongly types enums anyways) and use regular arithmetic to "iterate". Commented Oct 15, 2011 at 18:53
  • What does this have to do with templates? Commented Oct 15, 2011 at 19:03

2 Answers 2

6

No. Only static const integral data members can be initialized within a class.

However, you could do this...

template< typename A >
struct someClass
{
    enum State
    {
        sA,
        sB,
        sC
    };

    static const std::array<const State,4> the_states;
};

template<typename A>
const std::array<const someClass::State,4> someClass<A>::the_states = 
{
    someClass::State::sA, 
    someClass::State::sB, 
    someClass::State::sC 
};
Sign up to request clarification or add additional context in comments.

1 Comment

C++11 (which obviously is in effect here) allows this.
3
#include <iostream>
#include <array>

template< typename A >
struct someClass{
    enum class State {
        sA,
        sB,
        sC
    };

    static const std::array<State,3> the_states;    
};

template<typename A>
const std::array<typename someClass<A>::State,3> someClass<A>::the_states = {
    someClass<A>::State::sA,
    someClass<A>::State::sB,
    someClass<A>::State::sC
};

int main() {
    for( auto i : someClass<int>::the_states) {
        switch(i) {
            case someClass<int>::State::sA:
                std::cout << "sA" << std::endl;
                break;
            case someClass<int>::State::sB:
                std::cout << "sB" << std::endl;
                break;
            case someClass<int>::State::sC:
                std::cout << "sC" << std::endl;
                break;
        }
    }
}

note that you can't terminate the list with 0 the way you were trying with the 4 element array, because 0 cannot be converted to an enum class State.

Bah, and in the time it took me to edit my answer with the real answer Dave got it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.