提问人:YSC 提问时间:11/6/2023 更新时间:11/6/2023 访问量:59
名称注入失败:为什么找不到基构造函数的名称?[复制]
Name injection failure: why isn't the base constructor's name found? [duplicate]
问:
这个问题在这里已经有答案了:
在基类模板中查找依赖名称 (1 个答案)
这是否必须>访问派生类的 Base<T> 标识符? (2 个答案)
从模板基类派生时找不到类型 (1 个答案)
模板:父类成员变量在继承类中不可见 (3 个答案)
18天前关闭。
gcc 和 clang 都同意:只有当派生类派生自显式(“硬编码”)专用化时,才会注入模板化类的基构造函数名称。
有人可以理解并解释为什么吗?
接受
template<int Size>
struct Base
{};
template<int Size>
struct Derived : Base<0> // only difference here
{
Derived() : Base() {} // line 8
};
int main()
{
(void) Derived<0>{};
}
拒绝
template<int Size>
struct Base
{};
template<int Size>
struct Derived : Base<Size> // only difference here
{
Derived() : Base() {} // line 8, must be ': Base<Size>' although the name 'Base' should be injected in Derived.
};
int main()
{
(void) Derived<0>{};
}
跟8:17: error: class 'Derived<Size>' does not have any field named 'Base'
答: 暂无答案
下一个:迭代器构造函数的推导模板参数
评论
this->
Base::
Derived::
Derived::Base