gpt4 book ai didi

c++ - CRTP 中的复制赋值运算符 - gcc vs clang 和 msvc

转载 作者:行者123 更新时间:2023-11-30 04:56:00 24 4
gpt4 key购买 nike

以下代码(参见 this question)在 gcc 8.2 编译时会导致错误,但会被 clang 7.0.0 和 msvc 15.9.0 预览版 3 接受:

template<typename T>
struct B
{
void operator=(const T&) { }
void foo() { }
};

struct D : public B<D>
{
using B<D>::operator=;
using B<D>::foo; // hidden by D::foo
void foo() { }
};

int main()
{
D d1, d2;
d1 = d2;
d1.foo();
return 0;
}

错误信息generated by gcc是:

<source>: In function 'int main()':
<source>:8:8: error: 'constexpr D& D::operator=(const D&)' cannot be overloaded with 'void B<T>::operator=(const T&) [with T = D]'
struct D : public B<D>
^
<source>:4:8: note: previous declaration 'void B<T>::operator=(const T&) [with T = D]'
void operator=(const T&) { }
^~~~~~~~

我们在D中有两个赋值运算符,第一个是默认生成的,第二个是using引入的。它们具有相同的签名,因此重载失败。但是,为什么基类中的 operator= 没有像 foo() 成员函数那样被派生类隐藏?

这是怎么回事?为什么只有 gcc 提示?它应该提示吗?

最佳答案

GCC 在这里是错误的:这是 specially called out作为没有发生过载的情况。 (当然,正如您所说,其他成员函数也不会发生这种情况。)

关于c++ - CRTP 中的复制赋值运算符 - gcc vs clang 和 msvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52805074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com