微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 是一个私人移动构造函数来防止移动?

C中的常见模式是使副本构造函数为private:
class A
{
    public:
        // ...
    private:
        A(const A&);
};

但是下面的代码将会编译(在C 11/14中):

A f();

auto a = f();

该标准包含有关自动生成移动构造函数的信息.我既没有访问标准也没有实际生成移动构造函数的编译器.我的问题是:我必须写

class A
{
    public:
        // ...
    private:
        A(const A&);
        A(const A&&);
};

以防止移动(和类似运算符)?

解决方法

But will the following code then compile (in C++11/14):

不,不会.用户声明的复制构造函数的存在应该阻止移动构造函数的隐式生成.根据C11标准第12.8 / 9段:

If the deFinition of a class X does not explicitly declare a move constructor,one will be implicitly declared
as defaulted if and only if

X does not have a user-declared copy constructor,

— X does not have a user-declared copy assignment operator,

— X does not have a user-declared move assignment operator,

— X does not have a user-declared destructor,and

— the move constructor would not be implicitly defined as deleted.

原文地址:https://www.jb51.cc/c/111111.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐