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.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...