ref
对于函数式编程(如 std::bind),是对参数直接拷贝,而不是引用,std::ref的引入就是为了解决一些传参问题
ref是个函数模板,用来构建一个reference_wrapper对象并返回,该对象拥有传入的elem变量的引用。如果参数本身是一个reference_wrapper类型的对象,则创建该对象的一个副本,并返回。
使用
#include <iostream>
#include <functional>
void f(int& n1, int& n2, const int& n3)
{
std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
++n1;
++n2;
}
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';
}
运行结果:
std::bind 使用的是参数的拷贝而不是引用,所以在执行 std::bind 后,在函数 f() 中 n1 的值仍然是 1,n2 和 n3 改成了修改的值,因此必须使用 std::ref 来进行引用绑定。
与 std::bind 类似,std::thread 也是必须通过 std::ref 来绑定引用进行传参,否则,形参的引用声明是无效的。
#include <iostream>
#include <thread>
void threadFunc(std::string &str, int a)
{
str = "change by threadFunc";
a = 13;
}
int main()
{
std::string str("hello");
int a = 1;
std::thread th(threadFunc, std::ref(str), a);
th.join();
std::cout<<"str = " << str << std::endl;
std::cout<<"a = " << a << std::endl;
return 0;
}
运行结果:
ref 和 引用的区别
#include <iostream>
#include <functional>
#include<vector>
using namespace std;
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;
}
int main() {
int n1 = 1, n2 = 10, n3 = 100;
int& r1 = n1;
int& r2 = n2;
function<void()> f1 = bind(f, r1, r2, ref(n3));
f1();
cout << "out function a = " << n1 << " b = " << n2 << " c = " << n3 << endl;
cout << "out function a = " << &n1 << " b = " << &n2 << " c = " << &n3 << endl;
f1();
cout << "out function a = " << n1 << " b = " << n2 << " c = " << n3 << endl;
cout << "out function a = " << &n1 << " b = " << &n2 << " c = " << &n3 << endl;
return 0;
}
运行结果: