c++ - 将 'typedef' 从基类传播到 'template' 的派生类

标签 c++ templates inheritance typedef name-lookup

我正在尝试定义仅包含 typedef 的基类。

template<typename T>
class A
{
public:
    typedef std::vector<T> Vec_t;
};


template<typename T>
class B : public A<T>
{
private:
    Vec_t v;  // fails - Vec_t is not recognized
};

为什么在 B 中我收到 Vec_t 无法识别的错误,我需要明确地写它?

typename A<T>::Vec_t v;

最佳答案

我相信这个问题是重复的,但我现在找不到了。 C++ 标准规定您应该根据 14.6.2/3 完全限定名称:

In the definition of a class template or a member of a class template, if a base class of the class template depends on a template-parameter, the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member.

UPD: 我终于找到了重复项:here it is .

关于c++ - 将 'typedef' 从基类传播到 'template' 的派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23221841/

相关文章:

c++ - _Pass_fn : where is the constructor?

c++ - 使用SSE最快缩小8位灰度图像

c++ - 在C++中,将左值完美转发到函数模板的正确方法是什么?

c++ - 类定义中构造函数的模板特化

WPF 控件继承

c++ - 从多个组件构建一个 C++ 类

c++ - 串行 Mac OS X 不断卡住/锁定/消失 USB 到 Arduino

c++ - 整型/实型的模板结构特化

c++ - Makefile 中的 foreach 找不到所有文件

c++ - 使用派生类实例作为默认参数值