【发布时间】:2017-12-02 10:02:37
【问题描述】:
template <class Derived>
struct Base {
typedef typename Derived::T T;
};
template <typename T_>
struct Impl : public Base<Impl<T_>> {
typedef T_ T;
};
当初始化这个时,我得到一个错误
“Impl”中没有名为“T”的类型
那么,我怎样才能得到从基类派生的类型名呢?
【问题讨论】:
-
这不是关于 `typename' 关键字本身,而是关于 typename 的可见性。
-
我会说这是一种循环定义,你在 Impl 中定义了两次 T,你的意思是
struct Impl : public Base<T_> {? -
@nsubiron 您可以将第一个 typedef 更改为
typedef typename Derived::T T2;。不会真正影响错误。 -
没错,问题不在于重名。我的猜测是,在生成 Base 模板时,您还不了解 Impl 的内部。编译器首先需要生成 Base 类才能创建 Impl,所以此时 Impl 还没有 T 类型