gpt4 book ai didi

c++ - move 构造函数和 move 赋值运算符与复制省略

转载 作者:搜寻专家 更新时间:2023-10-31 01:25:56 28 4
gpt4 key购买 nike

相关问题:

我发布这个问题是因为 move 语义这个东西真的让我感到困惑。起初它们对我来说似乎很清楚,但当我试图向自己演示它们的用法时,我意识到也许我误解了什么。

我已经尝试将以下文件安排为一个使用 move 语义的类 vector 类的不那么简单的实现(实际上还有一个 main 函数,以及一个自由函数来使打印到屏幕更容易,...)。这并不是一个真正的最小工作示例,但它在屏幕上产生的输出是合理可读的,恕我直言。

不过,如果您认为最好将它瘦下来,请建议我该怎么做。

无论如何,代码如下,

#include<iostream>
using namespace std;

int counter = 0; // to keep count of the created objects

class X {
private:
int id = 0; // hopefully unique identifyier
int n = 0;
int * p;
public:
// special member functions (ctor, dtor, ...)
X() : id(counter++), n(0), p(NULL) { cout << "default ctor (id " << id << ")\n"; }
X(int n) : id(counter++), n(n), p(new int[n]) { cout << "param ctor (id " << id << ")\n"; };
X(const X& x) : id(counter++), n(x.n), p(new int[n]) {
cout << "copy ctor (id " << id << ") (allocating and copying " << n << " ints)\n";
for (int i = 0; i < n; ++i) {
p[i] = x.p[i];
}
};
X(X&& x) : id(counter++), n(x.n), p(x.p) {
cout << "move ctor (id " << id << ")\n";
x.p = NULL;
x.n = 0;
};
X& operator=(const X& x) {
cout << "copy assignment (";
if (n < x.size() && n > 0) {
cout << "deleting, ";
delete [] p;
n = 0;
}
if (n == 0) {
cout << "allocating, and ";
p = new int[n];
}
n = x.size();
cout << "copying " << n << " values)";
for (int i = 0; i < n; ++i) {
p[i] = x.p[i];
}
cout << endl;
return *this;
};
X& operator=(X&& x) {
this->n = x.n;
this->p = x.p;
x.p = NULL;
x.n = 0;
cout << "move assignment (\"moving\" " << this->n << " values)\n";
return *this;
};
~X() {
cout << "dtor on id " << id << " (array of size " << n << ": " << *this << ")\n";
delete [] p;
n = 0;
}
// getters/setters
int size() const { return n; }

// operators
int& operator[](int i) const { return p[i]; };
X operator+(const X& x2) const {
cout << "operator+\n";
int n = min(x2.size(), this->size());
X t(n);
for (int i = 0; i < n; ++i) {
t.p[i] = this->p[i] + x2.p[i];
}
return t;
};

// friend function to slim down the cout lines
friend ostream& operator<<(ostream&, const X&);
};

int main() {
X x0;
X x1(5);
X x2(5);
x1[2] = 3;
x2[3] = 4;
cout << "\nx0 = x1 + x2;\n";
x0 = x1 + x2;
cout << "\nX x4(x1 + x2);\n";
X x4(x1 + x2);
cout << x4 << endl;
cout << '\n';
}

// function to slim down the cout lines
ostream& operator<<(ostream& os, const X& x) {
os << '[';
for (int i = 0; i < x.size() - 1; ++i) {
os << x.p[i] << ',';
}
if (x.size() > 0) {
os << x.p[x.size() - 1];
}
return os << ']';
}

当我编译并运行它时

$ clear && g++ moves.cpp && ./a.out

输出如下(# - 手动添加注释)

default ctor (id 0)
param ctor (id 1)
param ctor (id 2)

x0 = x1 + x2;
operator+
param ctor (id 3)
move assignment ("moving" 5 values)
dtor on id 3 (array of size 0: [])

X x4(x1 + x2);
operator+
param ctor (id 4)
[0,0,3,4,0]

dtor on id 4 (array of size 5: [0,0,3,4,0])
dtor on id 2 (array of size 5: [0,0,0,4,0])
dtor on id 1 (array of size 5: [0,0,3,0,0])
dtor on id 0 (array of size 5: [0,0,3,4,0])

从输出的第一部分来看,我想我确实展示了 move 赋值运算符的预期用途。在这方面我是对的吗? (从下一个输出来看,我似乎不是,但我不确定。)

在这一点上,如果我关于复制省略阻止了对复制构造器的调用的推论是正确的,那么一个问题对我来说很自然 (and not only me, see OP's comment here):

基于另一个临时对象(例如 x4 基于 x1 + x2 中的 X x4(x1 + x2); 的结果)创建对象的情况不正是将语义 move 到应该引入的地方?如果不是,展示 move 构造器用法的基本示例是什么?

然后我读到可以通过添加适当的选项来防止复制省略。

输出

clear && g++ -fno-elide-constructors moves.cpp && ./a.out 

但是,是下面的:

default ctor (id 0)
param ctor (id 1)
param ctor (id 2)

x0 = x1 + x2;
operator+
param ctor (id 3)
move ctor (id 4)
dtor on id 3 (array of size 0: [])
move assignment ("moving" 5 values)
dtor on id 4 (array of size 0: [])

X x4(x1 + x2);
operator+
param ctor (id 5)
move ctor (id 6)
dtor on id 5 (array of size 0: [])
move ctor (id 7)
dtor on id 6 (array of size 0: [])
[0,0,3,4,0]

dtor on id 7 (array of size 5: [0,0,3,4,0])
dtor on id 2 (array of size 5: [0,0,0,4,0])
dtor on id 1 (array of size 5: [0,0,3,0,0])
dtor on id 0 (array of size 5: [0,0,3,4,0])
+enrico:CSGuild$

看起来我期望的对 move 构造函数的调用现在就在那里,但是那个调用和对 move 赋值的调用之前分别有另一个对 move 构造函数的调用。

为什么会这样?我是否完全误解了 move 语义的含义?

最佳答案

您在这里似乎有两个问题:

  • 为什么不为 X x4(x1 + x2) 调用 move 构造函数?
  • 为什么在禁用复制省略时, move 构造函数会被调用两次?

第一个问题

Isn't that situation (X x4(x1 + x2);) exactly the one for which move semantics where supposed to be introduced?

嗯,不。为了使用 move 语义,您有效地建议我们应该选择在 operator+构造一个 X,然后 move 它到结果 x4,这与在 operator+

第二个问题

禁用复制省略后,为什么我们在 X x4(x1 + x2) 期间看到对 move 构造函数的两次调用?考虑到这里有三个作用域:

  1. operator+ 作用域,我们在其中构造一个X 并返回它;
  2. 我们调用 X x4(x1 + x2)main 作用域;
  3. X 构造函数,我们从 x1 + x2 构造一个 X

然后,在没有省略的情况下,编译器是:

  • 将结果从 operator+ move 到 main(进入 x1 + x2);和
  • x1 + x2 的内容 move 到 x4

关于c++ - move 构造函数和 move 赋值运算符与复制省略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56104100/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com