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/37345326/

相关文章:

c++ - 从模板参数推导基本类型

c++ - 了解构造函数的一些具体情况

C++——如何使用 switch 语句和 constexprs 获得一个可用的货币转换器?

c++ - 使用 AES 加密 QDataStream

c++ - 如何使用 shellscalingapi.h 解决 C++ Windows 应用程序中的此链接器错误

c++ - 从重载函数中提取返回类型

c++ - 如何将 boost::intrusive_ptr 用于类模板中的私有(private)嵌套类

java - 使私有(private)字段在继承的子类中可见

c++ - 配置类也使用继承时的继承

C++从具有多个分隔符的文件中读取矩阵