首页 > 解决方案 > 我应该移动 std::exchange ed 成员吗?

问题描述

std::exchange用于实现移动构造函数。这是来自 cppreference.com https://en.cppreference.com/w/cpp/utility/exchange#Notes的示例。

但是,可能的std::exchange外观如下所示:

template<class T, class U = T>
T exchange(T& obj, U&& new_value)
{
    T old_value = std::move(obj);
    obj = std::forward<U>(new_value);
    return old_value;          // can be copy (until C++17) or a move (C++17), right?
}

现在我的情况:

#include <string>
#include <utility>

struct MyClass
{
    std::string m_str;
    // some other non-primitive members of the class

    MyClass(MyClass&& other) : m_str{ std::exchange(other.m_str, {}) } // enough?
        // or
        // : m_str{ std::move(std::exchange(other.m_str, {})) }
        //          ^^^^^^^^^^    do i need to move?                          
    {}

    MyClass& operator=(MyClass&& other)
    {
        this->m_str = std::exchange(other.m_str, {}); // enough?
        // or 
        // this->m_str = std::move( std::exchange(other.m_str, {}) );
        //               ^^^^^^^^^^    do I need to move?   
        return *this;
    }
};

正如我在代码中评论的那样,有机会按行移动或复制

m_str{ std::exchange(other.m_str, {}) }
this->m_str = std::exchange(other.m_str, nullptr);

所以,

我正在使用带有编译器标志 C++14 的 Visual Studio 2017。

标签: c++c++14move-constructor

解决方案


不,std::move这里不需要使用。经验法则是 - 如果某些返回值未分配给变量,它将被移动。

template<class T, class U = T>
T exchange(T& obj, U&& new_value)
{
    T old_value = std::move(obj);
    obj = std::forward<U>(new_value);
    return old_value;          // will be moved if move constructor defined
    // or even copy will be elided and will be no constructor call
}

与你所说的相反,这一举动在这里得到保证。C++17 更改了复制省略规则,但这是不同的

这里你可以看到prvalue是:

函数调用或重载的运算符表达式,其返回类型为非引用,例如 str.substr(1, 2)、str1 + str2 或 it++

纯右值的属性(作为右值的子集)是(强调我的):

右值可用于初始化右值引用,在这种情况下,由右值标识的对象的生命周期会延长,直到引用范围结束。

当用作函数参数并且函数的两个重载可用时,一个采用右值引用参数,另一个采用左值引用对 const 参数,右值绑定到右值引用重载(因此,如果复制和移动构造函数都可用,右值参数调用移动构造函数,同样使用复制和移动赋值运算符)。


推荐阅读