【问题标题】:c++11 use inner class and/or enum class in base templatec++11在基本模板中使用内部类和/或枚举类
【发布时间】:2016-05-12 04:40:23
【问题描述】:

我有一个继承模板的类。

class Foo : public Base<...> {
};

是否可以以及如何在基本模板中使用枚举类:

class Foo : public Base<EType> {
    enum class EType {
        kI1, kI2, kI3
    };
};

请注意,我很清楚将枚举从类中取出将允许我在模板中使用它。

【问题讨论】:

  • EType 在类定义开始之前需要有一个已知值。但事实并非如此,所以这段代码是一个 catch-22。你想完成什么?
  • 正是我要问的 - 有内部类,我还需要传递给 Base 模板。

标签: templates c++11 enums


【解决方案1】:

不,你不能这样做:

class Foo : public Base<EType> {
    enum class EType {
        kI1, kI2, kI3
    };
};

但你可以这样做:

enum class EType {
    kI1, kI2, kI3
};

class Foo : public Base<EType> {
};

如果需要,您可以将它们都放入一个命名空间中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多