C++ std::ref demo

std::ref()构建一个reference_wrapper对象并返回,该对象拥有传入的变量的引用。如果该对象本身就是reference_wrapper类型,则创建该对象的一个副本并返回。

在非模版的情况下,ref不能实现引用传递,只有模版自动推导类型或类型隐式转换时才行。ref能用包装类型reference_wrapper来代替原本会被识别的值类型,而reference_wrapper能隐式转换为被引用的值的引用类型

#include <iostream>
#include <functional>
#include <vector>
#include <thread>

using namespace std;

// 使用场景
// 1、std::ref在函数式编程(如std::bind)的使用时,是对参数直接拷贝,而不是引用
// 2、在多线程时传递参数使用

void f(int& a, int& b, int& c)
{
    cout << "in function a = " << a << "  b = " << b << "  c = " << c << endl;
    cout << "in function a = " << &a << "  b = " << &b << "  c = " << &c << endl;
    a += 1;
    b += 10;
    c += 100;
}

void f(int &a) { a += 5;}

int main() {
	int a = 5;
	thread th(f, std::ref(a));
	th.join();
	cout << a << endl;			// a = 10;

    int n1 = 1, n2 = 10, n3 = 100;
    int r1 = n1;
    int& r2 = n2;

    function<void()> f1 = bind(f, r1, r2, ref(n3));
//    即便是引用类型,bind 传入的还是其值的拷贝,第三个参数传入 reference_wrapper 对象,该对象可隐式的转换为值的引用

    f1();
    cout << "out function a = " << n1 << "  b = " << n2 << "  c = " << n3 << endl;
    cout << "out function a = " << &n1 << "  b = " << &n2 << "  c = " << &n3 << endl;

    r1 = 100;
    r2 = 100;                                                                                                                                                                                               

    f1();
    cout << "out function a = " << n1 << "  b = " << n2 << "  c = " << n3 << endl;
    cout << "out function a = " << &n1 << "  b = " << &n2 << "  c = " << &n3 << endl;

	/*
	in function a = 1  b = 10  c = 100
	in function a = 0x55763d32be84  b = 0x55763d32be80  c = 0x7fff1ba6fa18
	out function a = 1  b = 10  c = 200
	out function a = 0x7fff1ba6fa10  b = 0x7fff1ba6fa14  c = 0x7fff1ba6fa18
	in function a = 2  b = 20  c = 200
	in function a = 0x55763d32be84  b = 0x55763d32be80  c = 0x7fff1ba6fa18
	out function a = 1  b = 100  c = 300
	out function a = 0x7fff1ba6fa10  b = 0x7fff1ba6fa14  c = 0x7fff1ba6fa18
	*/

    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值