了解此示例中的移动构造函数行为(understand the move constructor behavior in this example)

编程入门 行业动态 更新时间:2024-10-25 00:34:09
了解此示例中的移动构造函数行为(understand the move constructor behavior in this example)

我正在学习C ++ 11中的移动语义。 我写了一个小程序来测试移动语义的行为。 但它不像我预期的那样行事,有人能解释我为什么吗?

#include<iostream> using namespace std; class Vector { public: Vector() { cout << "empty Ctor"<<endl; array = new int[10]; size = 10; } Vector(int n) { array = new int[n]; size = n; for (int i=0; i<size; ++i) array[i] = i; cout << "Ctor"<<endl; } Vector(const Vector& v):size(v.size) { array = new int[size]; for (int i=0; i<size; ++i) array[i] = v.array[i]; cout << "copy"<<endl; } Vector(Vector&& v):size(v.size) { array = v.array; v.array = nullptr; cout << "move"<<endl; } ~Vector() { delete array; } private: int* array; int size; }; int main() { Vector v(10); //print Ctor. (as expected) Vector v1(std::move(v)); //print move. (as expected) Vector v2(*(new Vector(2))); //print Ctor, copy. (I expect Ctor, move) Vector v3(Vector(2)); //print only Ctor. (I expect Ctor, move) }

那么,为什么印刷品不符合我的预期。 因为我认为传递给v2和v3的值都是Rvalue。 而对于v3,为什么只打印Ctor而没有打印“移动”或“复制”

I am learning the move semantics in C++11. I wrote a small program to test the behavior of move semantics. But it does not behave as what I expected, could someone explain me why?

#include<iostream> using namespace std; class Vector { public: Vector() { cout << "empty Ctor"<<endl; array = new int[10]; size = 10; } Vector(int n) { array = new int[n]; size = n; for (int i=0; i<size; ++i) array[i] = i; cout << "Ctor"<<endl; } Vector(const Vector& v):size(v.size) { array = new int[size]; for (int i=0; i<size; ++i) array[i] = v.array[i]; cout << "copy"<<endl; } Vector(Vector&& v):size(v.size) { array = v.array; v.array = nullptr; cout << "move"<<endl; } ~Vector() { delete array; } private: int* array; int size; }; int main() { Vector v(10); //print Ctor. (as expected) Vector v1(std::move(v)); //print move. (as expected) Vector v2(*(new Vector(2))); //print Ctor, copy. (I expect Ctor, move) Vector v3(Vector(2)); //print only Ctor. (I expect Ctor, move) }

So, why the print is not what I expected. Since I think both value passed to v2 and v3 are Rvalue. And for v3, why it print only Ctor without printing "move" or "copy"

最满意答案

Vector v2(*(new Vector(2)));

new Vector(2)是一个右值,但解引用它会生成左值,因此是复制而不是移动。

Vector v3(Vector(2));

由于临时Vector是不必要的,复制将被编译器忽略 。 您的编译器可能有一个标志来禁用复制elision,以便您可以看到额外的移动,例如GCC和Clang中的-fno-elide-constructors elide -fno-elide-constructors 。

Vector v2(*(new Vector(2)));

new Vector(2) is an rvalue, but dereferencing it produces an lvalue, hence the copy rather than the move.

Vector v3(Vector(2));

Since the temporary Vector is unnecessary, the copy will be elided by the compiler. Your compiler may have a flag to disable copy elision so that you can see the additional move, e.g. -fno-elide-constructors in GCC and Clang.

更多推荐

本文发布于:2023-04-28 03:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329819.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   函数   understand   behavior   constructor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!