加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:11:49 所属栏目:百科 来源:网络整理
导读:我认为当我删除B中的移动构造函数时,下面的代码仍然可以正常编译,因为它仍然需要复制构造函数来构造B对象.为什么编译器现在抱怨.没有= delete它没有并且无论如何都调用了复制构造函数,因为它不允许提供默认的移动构造函数!) class B{ public: B(){} ~B(){}
我认为当我删除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 ]

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读