error: T does not name a type - For specialisation using strongly typed enums

error: T does not name a type - For specialisation using strongly typed enums

我试图通过拥有一个主要包含静态函数和成员的基础 class 来避免重复代码。然后我会在其他一些 classes 中从基础 class 派生,同时使用来自基础 class.

的代码
#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;
}

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

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

constexpr Type Tp = T;