Typedef和模板特化

uj2*_*uj2 2 c++ templates type-systems typedef template-specialization

考虑以下代码:

typedef int type1;
typedef int type2;

template <typename>
struct some_trait;

template <>
struct some_trait<type1>
{
    static const int something=1;
};

template <>
struct some_trait<type2>
{
    static const int something=2;
};
Run Code Online (Sandbox Code Playgroud)

它失败了,因为编译器看到的是两个特化some_trait<int>.

最好的方法是什么?

Mic*_*urr 7

我认为能够专注于名称type1,type2你必须使它们实际上是不同类型而不是相同类型的别名:

struct type1 {
     int data;
};

struct type2 {
    int data;
};

// ...
Run Code Online (Sandbox Code Playgroud)

但如果你更多地解释你的最终目标,可能会有不同的/更好的方法来完成你正在寻找的东西.