如何使用在同一类的基模板中的类中声明的枚举

How do I use an enum declared in a class in that same class's base's template

本文关键字:枚举 声明 何使用 一类      更新时间:2023-10-16

对不起,wordy/延长的问题,但我不知道该怎么说。

我有一堂课。此类扩展了一个模板的基类。我想将在派生类中声明的枚举用作模板参数之一。这不是从中的价值,即实际的枚举,尽管我怀疑这有所作为。如果我尝试这样做,我会得到一个未确定的标识符错误,例如在这个简化的示例中。

template<class Value>
class Base
{
 public:
    Value foo;
}
class Derived : public Base<Derived::Colors>
{
    enum Colors
    {
        blue,
        red,
    }
}

我已经尝试环顾四周,发现枚举在班级中无法进行,所以我不知道该怎么办。似乎是一个很大的监督,使这样的用例不支持,所以有办法做到吗?在我的实际代码中,我宁愿将这个枚举保留在我的班级中,因为它与其功能密切相关。

因此,事实证明,至少在C 11中,不可能做到这一点。一个人需要做类似的事情:

template<class Value>
class Base
{
 public:
    Value foo;
}
// Does not compile
enum Derived::Colors;
class Derived : public Base<Derived::Colors>
{
    enum Colors
    {
        blue,
        red,
    }
}

在将其添加为模板参数之前,要知道Colors。但是,由于前进的宣言与枚举合作,这是不可能的。基本上,如果它们在班级内部声明,则标准明确地不允许在该课堂之外声明。