模板基类初始化 [英] template Base Class initialization
问题描述
在Visual C ++中,以下代码被接受时,g ++将生成错误:派生的类没有任何字段名称Base"哪个符合标准?
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(){}
};
顺便说一句:都接受
Derived():Base<T>(){}
所以同时,我将关注gcc
so meantime, I will follow gcc
推荐答案
MSVC ++不正确. Base
是模板,而不是类型.
MSVC++ is not correct. Base
is a template, not a type.
请注意,在通常情况下, Base
是在 Derived< T>
的范围内查找的,这意味着它将首先找到从其继承的注入的类名. Base< T>
,指的是 Base< T>
类型.但是由于您有一个依赖的基类,所以找不到从 Base< T>
继承的名称(未研究基类范围).
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).
这篇关于模板基类初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!