C++ 右值引用、构造函数和完美转发 的 一个问题

论坛 期权论坛 期权     
IT男之旭旭的故事   2019-6-30 05:30   2739   0
[h1]背景[/h1]最近同学想到一个问题,有关C++的右值引用。我本以为自己读了两遍C++ Primer,总该得心应手,却发现越解释,越迷糊。
这篇文章不会介绍有关右值引用和移动构造函数的概念,而是直接通过描述同学的问题并深入探讨,来尝试解释一些右值引用、移动构造和完美转发的具体用途。
[h1]问题[/h1]STL里面的std::map::insert,有两个
  1. insert
复制代码
方法的重载:
  • 自C++11
    1. template std::pair insert(P&& value);
    复制代码
  • 自C++17
    1. std::pair insert(value_type&& value);
    复制代码
这两个方法有什么不同用途?为什么在C++17时,又增加了第二个方法重载?
[h1]一些知识点[/h1]
    1. std::move
    复制代码
    只是一个到右值的类型转换,并不做任何其他实质性的事情。
  • 右值引用的右值引用可以合成为右值引用,其他的引用组合都只能合称为左值引用:
    1. typedef int&  lref;typedef int&& rref;int n;lref&  r1 = n; // type of r1 is int&lref&& r2 = n; // type of r2 is int&rref&  r3 = n; // type of r3 is int&rref&& r4 = 1; // type of r4 is int&&
    复制代码
  • 右值引用变量在表达式中是左值:
    1. int&& x = 1;f(x);            // calls f(int& x)f(std::move(x)); // calls f(int&& x)
    复制代码
  • 转发引用是一种特殊的引用,可以保留函数参数的原始类别。
    比如,模板函数参数声明为没有const/volatile修饰符的右值引用类型:
    1. templateint f(T&& x) {                    // x is a forwarding reference    return g(std::forward(x)); // and so can be forwarded}int main() {    int i;    f(i); // argument is lvalue, calls f(int&), std::forward(x) is lvalue    f(0); // argument is rvalue, calls f(int&&), std::forward(x) is rvalue}
    复制代码
[h1]实验 1[/h1]想搞清楚那两个方法的用途,最好的办法就是用一下他们。
不过
  1. map::insert
复制代码
方法过于复杂,我们写一个简单的程序,有类似的方法:
这里可以简单浏览代码,后面会详细展开解释。
[code]#include #include #include using namespace std;struct A{    string str;    A(const A& a) : str(a.str)    {        cout
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:5
帖子:1
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP