constexpr初始值设定项和枚举模板参数

时间:2016-05-22 06:41:14

标签: templates c++11 enums constexpr

当我使用constexpr初始值设定项和枚举模板论证时,我注意到我的代码中有些奇怪的东西。例如,以下代码将编译:

enum class TestEnum1 {
    Val1,
    Val2
};

template <TestEnum1 EnumVal> class TestClass1 {
    public:
        constexpr TestClass1() {
        }
};

class TestClass2 {
    public:
        TestClass1<TestEnum1::Val1> TestObject = TestClass1<TestEnum1::Val1>();
};

int main() {
    TestClass2 testObj;
}

而以下不会:

enum class TestEnum1 {
    Val1,
    Val2
};

template <typename T, TestEnum1 EnumVal> class TestClass1 {
    public:
        constexpr TestClass1() {
        }
};

class TestClass2 {
    public:
        TestClass1<int, TestEnum1::Val1> TestObject = TestClass1<int, TestEnum1::Val1>();
};

int main() {
    TestClass2 testObj;
}

请注意,我只在现有模板之前添加了另一个模板参数。错误消息类似于:

error: 'enum class TestEnum1' is not a class or a namespace
error: expected ';' at end of member declaration
error: expected unqualified-id before '>' token
error: expected unqualified-id before ')' token
error: template argument 1 is invalid

如果有人能解释原因或至少解决问题,我很高兴。我使用g ++ 5.3和--std = c ++ 11和windows 10。

编辑:我刚刚测试enum,在哪些情况下仍然出现错误,但删除了错误列表中的第一项。经过测试int,一切都很顺利。

0 个答案:

没有答案
相关问题