c++ - c++11对于三种方法的规则如何实现 "... = default;"

标签 c++ c++11 canonical-form

当我学习 C++ 时,人们告诉我要始终至少实现三种方法的规则。

现在我看到了新的“... = default;”来自 c++0x 堆栈溢出,我的问题是:

Is there a c++11 standard implementation defined for those methods or is it compiler specific?

另外我想要一些精确度:

  • What does the implementation looks like in term of code? (if it's generic)
  • Does this have an advantage compared to my example implementation below?
  • If you don't use assignment/copy constructor, what does *... = delete* do precisly, what's the difference with declaring them private? Answer (from @40two)
  • Is the new default= different from the old default implementation?

免责声明:当我需要在我的方法中使用更多高级功能时,我肯定会自己实现它们。但是我习惯于实现赋值运算符和复制构造函数,即使我从未使用过它们,只是为了让编译器不用。


我曾经做过的事情:(编辑,@DDrmmr swap/move)

//File T.h
class T
{
  public:
    T(void);
    T(const T &other);
    T(const T &&other);
    T &operator=(T other);
    friend void swap(T &first, T &second);
    ~T(void);

  protected:
    int *_param;
};

//File T.cpp
T::T(void) :
  _param(std::null)
{}

T::T(T &other)
  : _param(other._param)
{}

T::T(T &&other)
  : T()
{
  swap(*this, other);
}

T &T::operator=(T other)
{
  swap(*this, other);
  return (*this);
}

friend void swap(T &first, T &second)
{
  using std::swap;

  swap(first._param, second._param);
}

T::~T(void)
{}

最佳答案

默认行为是:

  • 默认构造器(T()):调用 bases def。执行者和成员默认执行者。
  • Copy ctor ( T(const T&) ): 调用基础复制。 Actor 和成员复制 Actor 。
  • Move ctor ( T(T&&) ):调用碱基移动。 Actor 和成员移动 Actor 。
  • 赋值(T& operator=(const T&)):调用碱基赋值。和成员分配。
  • Transfer ( T& operator=(T&&) ):调用基地转移,成员转移。
  • 析构函数(~T()):调用成员析构函数和基类析构函数(倒序)。

对于内置类型(int 等)

  • 默认构造器:如果显式调用则设置为 0
  • 复制构造函数:按位复制
  • 移动构造函数:按位复制(源代码没有变化)
  • 赋值:按位复制
  • 传输:按位复制
  • 析构函数:什么都不做。

由于指针也是内置类型,因此这适用于 int*(不适用于它指向的内容)。

现在,如果你不声明任何东西,你的 T 类将只持有一个 int* ,它不拥有指向的 int,所以 T 的拷贝将只持有一个指向相同的指针诠释。这与 C++03 产生的行为相同。内置类型的默认实现移动是复制。因为类是按成员移动的(并且取决于成员是什么:只是内置函数的拷贝)

如果你必须改变这种行为,你必须连贯地做到这一点:例如,如果你想“拥有”你指向的东西,你需要

  • 一个初始化为 nullptr 的默认构造函数:这定义了一个我们稍后可以引用的“空状态”
  • 初始化给定指针的创建者构造函数
  • 一个复制构造函数初始化为指向的拷贝(这是真正的变化)
  • 删除尖头的dtor
  • 删除指针并接收指针的新拷贝的赋值

.

T::T() :_param() {}
T::T(int* s) :_param(s) {}
T(const T& s) :_param(s._param? new int(*s._param): nullptr) {}
~T() { delete _param; } // will do nothing if _param is nullptr

现在我们先不定义赋值,而是专注于移动: 如果你不声明它,因为你声明了拷贝,它将被删除:这使得 T 对象总是被复制,即使是临时的(与 c++03 相同的行为)

但是如果源对象是临时的,我们可以创建一个空的目的地并交换它们:

T::T(T&& s) :T() { std::swap(_param, s._param); }

这就是所谓的移动

现在赋值:在 C++11 之前 T& operator=(const T& s) 应该检查自赋值,使目标为空并接收一份尖锐的:

T& operator=(const T& s)
{
    if(this == &s) return *this; // we can shortcut
    int* p = new int(s._param); //get the copy ...
    delete _param; //.. and if succeeded (no exception while copying) ...
    _param = p; // ... delete the old and keep the copy
    return *this;
}

在 C++11 中,我们可以使用参数传递来生成拷贝,从而给出

T& operator=(T s) //note the signature
{ std::swap(_param, s._param); return *this; }

请注意,这在 C++98 中也有效,但如果 s 是临时的,传递拷贝将不会在传递移动中优化。这使得这样的实现在 C++98 和 C++03 中无利可图,但在 C++11 中却非常方便。

另请注意,无需为 T 专门化 std::swap:std::swap(a,b); 将起作用,实现为三个移动(不是复制)

实现交换函数的做法是针对 T 有很多成员的情况,在移动和分配中都需要交换。但它可以是一个普通的私有(private)成员函数。

关于c++ - c++11对于三种方法的规则如何实现 "... = default;",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24445167/

相关文章:

c++ - 委托(delegate)给默认的移动构造函数

直接调用 XML 规范化算法与作为 xml 数字签名的一部分调用时相比,XML 规范化算法会给出两种不同的结果?

java - 查找单词的规范形式(与 mergeSort、java 相关)

c++ - map 使用哪种形式更好

c++ - 重载运算符 : operand order when using C++ literals

c++ - 在C++中,方法和函数有什么区别

c++ - 为什么 cout << 不能与重载的 * 运算符一起工作?

c++ - 自动转换类型,当非静态函数的签名如此明显时

c++ - 在不同的范围内新建和删除

database - 使用阿姆斯特朗公理计算规范覆盖