Should a move constructor take a const or non-const rvalue reference?

前端 未结 4 952
死守一世寂寞
死守一世寂寞 2020-11-30 03:29

In several places I\'ve seen the recommended signatures of copy and move constructors given as:

struct T
{
    T();
    T(const T& other);
    T(T&&a         


        
4条回答
  •  有刺的猬
    2020-11-30 03:53

    It should be a non-const rvalue reference.

    If an object is placed in read-only memory, you can't steal resources from it, even if its formal lifetime is ending shortly. Objects created as const in C++ are allowed to live in read-only memory (using const_cast to try to change them results in undefined behavior).

提交回复
热议问题