其他分享
首页 > 其他分享> > c11 move 和 forward

c11 move 和 forward

作者:互联网

c++11 : move forward 个人理解

右值引用(rvale references)和全局引用(universal references)的区别:

T&&”有两种意思:

  1. 代表的是右值引用(rvalue reference)。就是他绑定到一个右值上,代表对象的移动来源。

2.代表可能是右值引用也可能是左值引用。叫做全局引用。

全局引用使用在两个地方:

1.函数模板:

  template<typename T> void f(T&& param)   //param is a universal reference

2.auto 的使用:

 auto&& var2 = var1;

使用右值引用(T&& 第一种意思)的情况:

void f(Widget&& param)  // no type deduction param is an  rvalue reference
Widget&& var1 = Widget(); // no type deduction param is an  rvalue reference

特殊情况:

  1. 虽然T需要类型推理,但是param是vector不是"T&&"
template<typename T>
void f(std::vector<T>&& param) // param is an rvalue reference
  1. 对与const关键字:
template <typename T>
void f(const T&& param);  //param is an rvalue reference

std::move使用在rvalue reference std::forward使用在universal reference

很严重的错误是将std::move应用到universal reference:会造成修改左值这样不愿意看到的错误:

class Widget{
  pulibc:
    templtea <typename T>
    void setName(T&& newName) //universal reference
    {
      name = std::move(newName); //compiles ,but is bad bad bad
    }
    ...
  private:
    std::string name;
}

std::string getWidgetName();

Widget w;
auto n = getWidgetName(); // moves n into w!
w.setName(n);    //n's vale now unkonow;

这样最后n的值变成不确定状态了。也许是觉得使用两个函数重载:

class Widget{
public:
    void setName(const std::string& newname){name = newName;}
    void set(std::string&& newName) {name = std::move(newName);}
}

这种情况也是可以的。但是有缺点是这中方式不是很有效。会有很多std::string 的构造函数,析构函数,复制构造函数的调用过程。同时会增加代码量。还有一个更严重的问题是代码设计问题。现在只是一个参数就需要连个函数,如果函数接收参数过多,这样会需要跟多的重载函数。

对应std::move 和 std::forward的理解:


std::move 和std::forward只是一个函数,该函数进行cast操作。move是无条件的转换为右值,而forward是有条件的转换。

当然,右值只是作为一个moving的候选:

class Annotation{
  public :
    explicit Annotation(const std::string text):
        value(std::move(text))  // "move "text into value,this code doesn't do what it seems to!!
        {...}
  private:
      std::string value;
}

"text"并没有被moved into value,而是被复制。虽然text被move转为了右值,但是text被声明为const std::string,转化为const std::string的右值,但是:

class string{
public:
  ...
  string (const string&rhs); //copy ctor
  string (string&& rhs); //move ctor
}

const std::string右值无法作为std::string 的move 构造函数。不过可以作为拷贝构造函数参数。因为lvalue-reference-to-const是运行邦迪哦那个到const rvalue。

所以得出两条结论:

  • 不要将一个对像设置为const,如果你希望该从对象moving。
  • std::move 并不移动任何东西,而去也不保证被cast的东西真正被moved。唯一确定的是std::move把他转换为右值。

std::forward是有条件的转换:只有当参数是右值的时候才被转换为右值。

标签:std,const,string,右值,move,&&,forward,c11
来源: https://www.cnblogs.com/ultramanX/p/14745617.html