【C++】子类访问父类typedef的问题

class A
{
public:
	typedef int* pointer;
};
class B :public A
{
public:
	pointer b;
};

这段代码运行没有问题,子类继承了父类定义的类型pointer。

但当普通类变成模板类时:

template<class T>
class A
{
public:
	typedef T* pointer;
};
template<class T>
class B :public A<T>
{

public:
	pointer b;
};

当定义一个B对象时,上述代码在g++中会报错:pointer does not name a type。
查阅资料后了解到:

N4567 § 14.6.2[temp.dep]p3
In the definition of a class or class template, the scope of a dependent base class (14.6.2.1) 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.
即A<T> 中的T是模板参数,这样的类型是 dependent type 。这里,A<T> 被用作基类,于是这种基类叫做 dependent base class 。按照 C++ 标准的规定,除非显式用 基类名::成员名 的语法,否则不会查找 dependent base class 的成员。

以下面代码为例:

typedef double A;
template<class T> class B {
    typedef int A;
};
template<class T> struct X : B<T> {
    A a; // 此处的A为double类型
};

所以如果要使用pointer类型,需写成**typename A<T>::pointer b; **

ps:在vs2013中却能正确编译通过

原文地址:https://www.cnblogs.com/cknightx/p/6718889.html