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

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

发布时间:2020-12-16 03:33:28 所属栏目:百科 来源:网络整理
导读:C中的常见模式是使副本构造函数为private: class A{ public: // ... private: A(const A);}; 但是下面的代码将会编译(在C 11/14中): A f();auto a = f(); 该标准包含有关自动生成移动构造函数的信息.我既没有访问标准也没有实际生成移动构造函数的编译器.
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.

(编辑:李大同)

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

    推荐文章
      热点阅读