English 中文(简体)
静态组装:
原标题:static const std::array in class

我有一套模版的构件,其中有一些缩略语。 是否有做以下工作的途径?

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
    }};

};
最佳回答

No. 只有静态综合数据成员才能在某一类别内初步形成。

然而,你可以这样做......

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 
};
问题回答
#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;
        }
    }
}

请注意,你可以以零敲碎打的方式终止名单,因为0人不能被转换成一个大类国家。

巴哈教徒,当我对真实的Dave先生的回答置之不理的时候。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签