c++ - 使用枚举实例化模板

标签 c++ templates enums constants

为什么以下编译会失败并显示 error: ‘arg’ cannot appear in a constant-expression

class Foo {
public:
    enum myenum { BIRDY, NUMNUM };
    typedef enum myenum myenum_t;
    void bar(const myenum_t arg);
}
template<Foo::myenum_t> class MyClass {};
void Foo::bar(const myenum_t arg) {
    MyClass<arg> hey;
}

枚举类型不是编译时常量吗?

最佳答案

您正在尝试使用变量,而不仅仅是常量,编译时没有变量,请使用模板函数。

template<myenum_t>
void bar();

template<Foo::myenum_t arg>
void Foo::bar()
{
   MyClass<arg> hey;
}

关于c++ - 使用枚举实例化模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29233461/

相关文章:

C++:矩阵类

c++ - 为 std::bind 创建模板包装器

Java 枚举和 Objective-C 枚举

c++ - 对模板函数的引用 vector

java - 如何在java中使字符串变量只有几个预定义值

java - 以枚举组合作为键的映射

c++ - 使用 `libopencv_ffmpeg.so` 在 Linux 上构建 OpenCV 2.4.11

c++ - std::is_constructible on type with non-public destructor

c++ - 定义模板构造器

c++ - 在 Concept 中使用带有 CRTP 的模板模板参数