错误:T未命名类型 - 对于使用强类型枚举的专业化

时间:2015-03-24 00:41:18

标签: c++ templates enums typedef

我试图通过拥有一个主要是静态函数和成员的基类来避免重复代码。然后,在使用基类中的代码时,我将从其他类中的基类派生。

#include <unordered_set>

enum struct Type
{
    A = 0, B, C
};

template <Type T> struct Impl;

template <Type T> struct Base
{
    typedef T Tp; // error: 'T' does not name a type

    typedef Impl<T> Imp;

    static std::unordered_set<Imp*> _Inst;

    static void grab(Imp * ptr) { _Inst.insert(ptr); }
    static void drop(Imp * ptr) { _Inst.erase(ptr); };
    static void swap(Imp * ptr, Imp * old) { drop(old); grab(ptr); }

    // ...
};

template <> struct Impl<Type::A> : public Base<Type::A>
{
    // ...
};

template <> struct Impl<Type::B> : public Base<Type::B>
{
    // ...
};

template <> struct Impl<Type::C> : public Base<Type::C>
{
    // ...
};

int main(int argc, char** argv)
{
    return 0;
}

实际实现与该示例有很大不同,需要我采用类似的方法。

1 个答案:

答案 0 :(得分:1)

T不是类型,您只能typedef种类型。创建成员变量。例如,

constexpr Type Tp = T;
相关问题