ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c11 move 和 forward

2021-05-08 17:01:15  阅读:231  来源: 互联网

标签:std const string 右值 move && forward c11


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有