std::ref

作者: gykimo | 来源:发表于2021-12-27 17:59 被阅读0次

原文:https://murphypei.github.io/blog/2019/04/cpp-std-ref

C++11 中引入 std::ref 用于取某个变量的引用,这个引入是为了解决一些传参问题。

我们知道 C++ 中本来就有引用的存在,为何 C++11 中还要引入一个 std::ref 了?主要是考虑函数式编程(如 std::bind)在使用时,是对参数直接拷贝,而不是引用。下面通过例子说明

示例1:

#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';
}

输出:

Before function: 10 11 12
In function: 1 11 12
After function: 10 12 12

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

之所以std::bind默认是拷贝,因为std::function<void()> bound_f,这个函数变量可能不在当前作用域下执行,如果在其他作用域,那么当前作用域退出时,n1、n2、n3就会被释放,bound_f再次调用 f 函数时,如果 f 操作的还是原来的n1、n2、n3很可能出现内存错误,所以默认是拷贝;当改为 std::ref 时,必须人为确认使用引用没有问题,才能这么写

示例2:

#include<thread>
#include<iostream>
#include<string>

void threadFunc(std::string &str, int a)
{
 str = "change by threadFunc";
 a = 13;
}

int main()
{
 std::string str("main");
 int a = 9;
 std::thread th(threadFunc, std::ref(str), a);

 th.join();

 std::cout<<"str = " << str << std::endl;
 std::cout<<"a = " << a << std::endl;

 return 0;
}

输出:

str = change by threadFunc
a = 9

可以看到,和 std::bind 类似,多线程的 std::thread 也是必须显式通过 std::ref 来绑定引用进行传参,否则,形参的引用声明是无效的。

相关文章

  • std::ref

    C++11引入了std::ref 为了解决函数式编程的参数是值拷贝的情况 以上的例子中 除了直接调用lambda ...

  • std::ref

    原文:https://murphypei.github.io/blog/2019/04/cpp-std-ref[h...

  • bind

    std::bind(&X::f, ref(x), std::placeholders::_1)(i); /...

  • stl std::ref std::cref

    https://zh.cppreference.com/w/cpp/utility/functional/ref ...

  • C++ concurrency in action: 1~4 章

    1 概述 2 管理线程: thread/join/detach/RAAI/std::ref/std::bind/m...

  • C++ concurrency in action: Key p

    1 概述 2 管理线程: thread/join/detach/RAAI/std::ref/std::bind/m...

  • std::ref()和&

    引言 最近看到一个多线程代码如下: 其中创建线程的部分使用了std::thread t1(accumulator_...

  • 转载--std::ref应用

    在std::promise范例中,使用了std::ref将future对象传递给引用参数类型的任务函数。 std:...

  • C++11为什么需要std::ref/reference_wra

    在std::promise范例中,使用了std::ref将future对象传递给引用参数类型的任务函数。 如果直接...

  • C++中隐式类型转换

    1 operator隐式类型转换 1.1 std::ref源码中reference_wrapper隐式类型转换 在...

网友评论

      本文标题:std::ref

      本文链接:https://www.haomeiwen.com/subject/xhwyqrtx.html