c++11 完美转发 std::forward()

其实对于 std::move来说,只做了一件事。可以初步的理解为 (不过当然是错误的)

template<typename T>
T&& move(T& val)
{
    return static_cast<T&&>(val);
}

move 只是纯粹的将一个左值转化为了一个右值,STL实现基本都已经实现了移动语义,相当于对于 vector<T>::push_back()有两个版本的实现,简单写如下:

template<typename T>
class Vector
{
    void push_back(T& lval);
    void push_back(T&& rval);
};

而对应的类型 T 也实现了移动拷贝,如下:

class T
{
    T(T& other)
    {
        // copy constructor
    }

    T(T&& other)
    {
        // move constructor
    }
};

代码有性能提升

完美转发 std::forward()

当我们将一个右值引用传入函数时,他在实参中有了命名,所以继续往下传或者调用其他函数时,根据C++ 标准的定义,这个参数变成了一个左值。那么他永远不会调用接下来函数的右值版本,这可能在一些情况下造成拷贝。为了解决这个问题 C++ 11引入了完美转发,根据右值判断的推倒,调用forward 传出的值,若原来是一个右值,那么他转出来就是一个右值,否则为一个左值。
这样的处理就完美的转发了原有参数的左右值属性,不会造成一些不必要的拷贝。代码如下:


#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    string A("abc");
    string&& Rval = std::move(A);
    string B(Rval);    // this is a copy , not move.
    cout << A << endl;   // output "abc"
    string C(std::forward<string>(Rval));  // move.
    cout << A << endl;       /* output "" */
    return 0;
}

猜你喜欢

转载自blog.csdn.net/coolwriter/article/details/80970718
今日推荐