7

看起来我是类之间的继承类型别名,而不是类模板之间的继承类型别名?我不明白为什么这段代码有效:

#include <iostream>

//template<typename T>
struct Test1
{
//    using t1=T;
    using t1=int;
};

//template<typename T>
struct Test2: public Test1//<T>
{
  t1 x;  
};

int main(int argc, char *argv[]) {
//    Test2<int> a;
    Test2 a;
    a.x=5;
    std::cout << a.x << std::endl;
}

并且此代码不会:

#include <iostream>

template<typename T>
struct Test1
{
    using t1=T;
};

template<typename T>
struct Test2: public Test1<T>
{
  t1 x;  
};

int main(int argc, char *argv[]) {
    Test2<int> a;
    a.x=5;
    std::cout << a.x << std::endl;
}

类型不通过模板继承吗?

4

1 回答 1

4

以下将起作用:

 typename Test1<T>::t1 x;

而且,正如 Xeo 在上面的评论中指出的那样:

typename Test2::t1 x;
于 2013-07-28T21:47:58.347 回答