问题
while in visual c++ the code below is accepted, g++ will generate the error: "class Derived does not have any field name Base" which is following the standard?
template <class T>
class Base
{
public:
Base(){};
};
template <class T>
class Derived:public Base<T>
{
public:
Derived():Base(){}
};
BTW: both accept
Derived():Base<T>(){}
so meantime, I will follow gcc
回答1:
MSVC++ is not correct. Base
is a template, not a type.
Note that in the usual case, Base
is looked up in the scope of Derived<T>
, which means that it will first find the injected class name inherited from Base<T>
, which refers to the type Base<T>
. But as you have a dependent base class, the name inherited from Base<T>
is not found (the base class scope is not looked into).
来源:https://stackoverflow.com/questions/5860783/template-base-class-initialization