名称注入失败:为什么找不到基构造函数的名称?[复制]

Name injection failure: why isn't the base constructor's name found? [duplicate]

提问人:YSC 提问时间:11/6/2023 更新时间:11/6/2023 访问量:59

问:

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'

Compiler Explorer 上的演示

C++ 模板 语言律师 查找名称 注入

评论

1赞 HolyBlackCat 11/6/2023
这与从基类继承任何其他标识符是一回事。如果基数依赖于模板参数,则不直接可见(因此您需要添加 或 )。例如,在这种情况下有效。this->Base::Derived::Derived::Base
1赞 user12002570 11/6/2023
因为在后者中,它是一个从属名称。基类模板中的依赖名称查找
0赞 YSC 11/6/2023
我忘了。我敢肯定某处有个骗子。我的错。
0赞 user12002570 11/6/2023
@YSC 从模板基类派生时找不到类型看起来像它,尽管我敢肯定还有更多。
0赞 YSC 11/6/2023
你做了你应有的挖掘。谢谢。

答: 暂无答案