What are move semantics?

I find it easiest to understand move semantics with example code. Let‘s start with a very simple string class which only holds a pointer to a heap-allocated block of memory:

#include <cstring>
#include <algorithm>

class string
{
    char* data;

public:

    string(const char* p)
    {
        size_t size = strlen(p) + 1;
        data = new char[size];
        memcpy(data, p, size);
    }

Since we chose to manage the memory ourselves, we need to follow the rule of three. I am going to defer writing the assignment operator and only implement the destructor and the copy constructor for now:

    ~string()
    {
        delete[] data;
    }

    string(const string& that)
    {
        size_t size = strlen(that.data) + 1;
        data = new char[size];
        memcpy(data, that.data, size);
    }

The copy constructor defines what it means to copy string objects. The parameter const string& that binds to all expressions of type string which allows you to make copies in the following examples:

string a(x);                                    // Line 1
string b(x + y);                                // Line 2
string c(some_function_returning_a_string());   // Line 3

Now comes the key insight into move semantics. Note that only in the first line where we copy x is this deep copy really necessary, because we might want to inspect x later and would be very surprised if x had changed somehow. Did you notice how I just said x three times (four times if you include this sentence) and meant the exact same object every time? We call expressions such as x "lvalues".

The arguments in lines 2 and 3 are not lvalues, but rvalues, because the underlying string objects have no names, so the client has no way to inspect them again at a later point in time. rvalues denote temporary objects which are destroyed at the next semicolon (to be more precise: at the end of the full-expression that lexically contains the rvalue). This is important because during the initialization of b andc, we could do whatever we wanted with the source string, and the client couldn‘t tell a difference!

C++0x introduces a new mechanism called "rvalue reference" which, among other things, allows us to detect rvalue arguments via function overloading. All we have to do is write a constructor with an rvalue reference parameter. Inside that constructor we can do anything we want with the source, as long as we leave it in some valid state:

    string(string&& that)   // string&& is an rvalue reference to a string
    {
        data = that.data;
        that.data = 0;
    }

What have we done here? Instead of deeply copying the heap data, we have just copied the pointer and then set the original pointer to null. In effect, we have "stolen" the data that originally belonged to the source string. Again, the key insight is that under no circumstance could the client detect that the source had been modified. Since we don‘t really do a copy here, we call this constructor a "move constructor". Its job is to move resources from one object to another instead of copying them.

Congratulations, you now understand the basics of move semantics! Let‘s continue by implementing the assignment operator. If you‘re unfamiliar with the copy and swap idiom, learn it and come back, because it‘s an awesome C++ idiom related to exception safety.

    string& operator=(string that)
    {
        std::swap(data, that.data);
        return *this;
    }
};

Huh, that‘s it? "Where‘s the rvalue reference?" you might ask. "We don‘t need it here!" is my answer :)

Note that we pass the parameter that by value, so that has to be initialized just like any other string object. Exactly how is that going to be initialized? In the olden days of C++98, the answer would have been "by the copy constructor". In C++0x, the compiler chooses between the copy constructor and the move constructor based on whether the argument to the assignment operator is an lvalue or an rvalue.

So if you say a = b, the copy constructor will initialize that (because the expression b is an lvalue), and the assignment operator swaps the contents with a freshly created, deep copy. That is the very definition of the copy and swap idiom -- make a copy, swap the contents with the copy, and then get rid of the copy by leaving the scope. Nothing new here.

But if you say a = x + y, the move constructor will initialize that (because the expression x + y is an rvalue), so there is no deep copy involved, only an efficient move. that is still an independent object from the argument, but its construction was trivial, since the heap data didn‘t have to be copied, just moved. It wasn‘t necessary to copy it because x + y is an rvalue, and again, it is okay to move from string objects denoted by rvalues.

To summarize, the copy constructor makes a deep copy, because the source must remain untouched. The move constructor, on the other hand, can just copy the pointer and then set the pointer in the source to null. It is okay to "nullify" the source object in this manner, because the client has no way of inspecting the object again.

I hope this example got the main point across. There is a lot more to rvalue references and move semantics which I intentionally left out to keep it simple. If you want more details please see my supplementary answer.

http://stackoverflow.com/questions/3106110/what-are-move-semantics

What are move semantics?

时间: 2024-10-19 12:05:33

What are move semantics?的相关文章

Move semantics(C++11)

/* * Compile with: *       g++ move_test.c -o move_test -std=c++11 -g -fno-elide-constructors * -fno-elide-constructors disabled the return value optimize. */ #include <iostream> #include <utility> class A { public: A(void) { a = new int; std:

我是如何明白C++的move semantics(右值引用)和perfect forwarding(完美转发)的

其实主要就是三篇文章(博客): 首推这篇. http://thbecker.net/articles/rvalue_references/section_01.html 从这里你可以知道什么时候你会知道,什么时候能够 “链式地” 调用移动构造函数而什么时候不能 ,明白其中过程(特别是什么时候不能)的一些细微差别,你就差不多懂什么是move semantic了. 然后是scott meyers的Universal Reference In C++11: https://isocpp.org/blo

C++11的value category(值类别)以及move semantics(移动语义)

转载请保留以下声明 作者:赵宗晟 出处:http://www.cnblogs.com/zhao-zongsheng/p/value_categories_and_move_semantics.html C++11之前value categories只有两类,lvalue和rvalue,在C++11之后出现了新的value categories,即prvalue, glvalue, xvalue.不理解value categories可能会让我们遇到一些坑时不知怎么去修改,所以理解value ca

C++11新特性之 Move semantics(移动语义)

按值传递的意义是什么? 当一个函数的参数按值传递时,这就会进行拷贝.当然,编译器懂得如何去拷贝. 而对于我们自定义的类型,我们也许需要提供拷贝构造函数. 但是不得不说,拷贝的代价是昂贵的. 所以我们需要寻找一个避免不必要拷贝的方法,即C++11提供的移动语义. 上一篇博客中有一个句话用到了: #include <iostream> void f(int& i) { std::cout << "lvalue ref: " << i <&l

c++11 shared_ptr &amp; unique_ptr &amp; move semantics(右值引用)

just read it smart_ptr: https://mbevin.wordpress.com/2012/11/18/smart-pointers/ 使用任何指针是都要考虑ownership+memory-management+lifetime这几个问题. who is the owner of this object? is there one owner or many? who will responese for the deletion? 使用unique_ptr则表示该对象

Rust: move和borrow

感觉Rust官方的学习文档里关于ownship,borrow和lifetime介绍的太简略了,无法真正理解这些语法设计的原因以及如何使用(特别是lifetime).所以找了一些相关的blog来看,总结一下,以备以后参考. 起因 Rust想要解决的问题是在无GC的情况下安全地管理资源.这点并不容易实现,但不是一点思路都没有.比如,有一个Java程序: public void foo() { byte[] a = new byte[10000000]; a = null; byte[] c = ne

C++ RVO/NRVO以及move语义的影响

C++返回值优化和具名返回值优化是编译器的优化,在大多数情况下能提高性能,但是却难以受程序员控制.C++11中加入了move语义的支持,由此对RVO和NRVO会造成一定影响.下面以一段代码来说明. RVO和NRVO在分别在copy/move construct,copy/move assignment八种简单情况,测试条件是g++ 4.8.2和clang++ 3.4,默认优化. #include <iostream> #include <vector> #include <s

Understand the Qt containers(有对应表)

Container classes are one of the cornerstones of object-oriented programming, invaluable tools that free us from having to permanently think about memory management. Qt comes with its own set of container classes, closely modeled after those in the S

详解C++右值引用

http://jxq.me/2012/06/06/%E8%AF%91%E8%AF%A6%E8%A7%A3c%E5%8F%B3%E5%80%BC%E5%BC%95%E7%94%A8/#thbecker C++0x标准出来很长时间了,引入了很多牛逼的特性[1].其中一个便是右值引用,Thomas Becker的文章[2]很全面的介绍了这个特性,读后有如醍醐灌顶,翻译在此以便深入理解. 目录 概述 move语义 右值引用 强制move语义 右值引用是右值吗? move语义与编译器优化 完美转发:问题