c++11之为什么C++11引入了std::ref?

C++本身有引用(&),为什么C++11又引入了std::ref?

  主要是考虑函数式编程(如std::bind)在使用时,是对参数直接拷贝,而不是引用。如下例子:

复制代码
#include <functional>
#include <iostream>

void f(int& n1, int& n2, const int& n3)
{
    std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << 'n';
    ++n1; // increments the copy of n1 stored in the function object
    ++n2; // increments the main()'s n2
    // ++n3; // compile error
}

int main()
{
    int n1 = 1, n2 = 2, n3 = 3;
    std::function<void()> bound_f = std::bind(f, n1, std::ref(n2), std::cref(n3));
    n1 = 10;
    n2 = 11;
    n3 = 12;
    std::cout << "Before function: " << n1 << ' ' << n2 << ' ' << n3 << 'n';
    bound_f();
    std::cout << "After function: " << n1 << ' ' << n2 << ' ' << n3 << 'n';
}
复制代码
Output:
Before function: 10 11 12
In function: 1 11 12
After function: 10 12 12

  上述代码在执行std::bind后,在函数f()中n1的值仍然是1,n2和n3改成了修改的值。说明std::bind使用的是参数的拷贝而不是引用。具体为什么std::bind不使用引用,可能确实有一些需求,使得C++11的设计者认为默认应该采用拷贝,如果使用者有需求,加上std::ref即可。

原文链接: https://www.cnblogs.com/wangshaowei/p/8874518.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    c++11之为什么C++11引入了std::ref?

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/391948

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月31日 上午10:51
下一篇 2023年3月31日 上午10:51

相关推荐