std::ref 用于包装按引用传递的值。
std::cref 用于包装按const 引用传递的值。
void f(int &n1,int &n2,const int& n3)
{
cout << "In : " << n1 << " " << n2 << " " << n3 << " " << endl;
n1++;
n2++;
}
int main()
{
int n1 = 1, n2 = 2, n3 = 3;
function<void()> bound_f = bind(f,n1,ref(n2),cref(n3));
n1 = 11;
n2 = 22;
n3 = 33;
cout << "Before : " << n1 << " " << n2 << " " << n3 << " " << endl;
bound_f();
cout << "After : " << n1 << " " << n2 << " " << n3 << " " << endl;
return 0;
}
输出:
Before : 11 22 33
In : 1 22 33
After : 11 23 33