template Base Class initialization

谁说我不能喝 提交于 2021-02-07 18:26:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!