c – 后备复制构造函数不工作?

c – 后备复制构造函数不工作?,第1张

概述我认为当我删除B中的移动构造函数时,下面的代码仍然可以正常编译,因为它仍然需要复制构造函数来构造B对象.为什么编译器现在抱怨.没有= delete它没有并且无论如何都调用了复制构造函数,因为它不允许提供默认的移动构造函数!) class B{ public: B(){} ~B(){} B & operator=(const B & b){ std:: 我认为当我删除B中的移动构造函数时,下面的代码仍然可以正常编译,因为它仍然需要复制构造函数来构造B对象.为什么编译器现在抱怨.没有= delete它没有并且无论如何都调用了复制构造函数,因为它不允许提供默认的移动构造函数!)

class B{    public:    B(){}    ~B(){}    B & operator=(const B & b){        std::cout << " cannot move -> copy " << std::endl;         return *this;    }    B(const B & v){        std::cout << " cannot move -> copy " << std::endl;            }    // B(B && b) = delete; // uncomment this!};int main(){    B b( B{} ); }

编译器输出与clang 3.6(Live code)

main.cpp:27:7: error: call to deleted constructor of 'B'    B b( B{} );      ^  ~~~main.cpp:21:5: note: 'B' has been explicitly marked deleted here    B(B && b) = delete;    ^1 error generated.
解决方法 仍然声明具有已删除定义的函数.除此之外,它通常会参与重载决策 – 但如果重载决议实际上选择它,则程序格式不正确([dcl.fct.def.delete] / 2):

A program that refers to a deleted function implicitly or explicitly,other than to declare it,is ill-formed.
[ Note: This includes calling the function implicitly or explicitly and forming a pointer or pointer-to-member
to the function. It applIEs even for references in Expressions that are not potentially-evaluated. If a function
is overloaded,it is referenced only if the function is selected by overload resolution. —end note ]

这与从未声明过的函数不同.当然,不存在的声明不参与重载决策.

总结

以上是内存溢出为你收集整理的c – 后备复制构造函数不工作?全部内容,希望文章能够帮你解决c – 后备复制构造函数不工作?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1227626.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存