石家庄网站建设电商,哪个网站可以免费做国外网站,正规网站建设方案详细,模版网站搭建参考#xff1a;
std::ref, std::cref - cppreference.comhttps://en.cppreference.com/w/cpp/utility/functional/ref
正文#xff1a;
如果不涉及函数式编程#xff0c;那么基本上不需要使用到 std::ref #xff0c; 这个功能式是用来解决函数式编程时入参只能进行值传…参考
std::ref, std::cref - cppreference.comhttps://en.cppreference.com/w/cpp/utility/functional/ref
正文
如果不涉及函数式编程那么基本上不需要使用到 std::ref 这个功能式是用来解决函数式编程时入参只能进行值传递的问题的不过如果使用指针则同样不需要 std::ref如果不用指针则大概率会需要。
#include functional
#include iostreamvoid 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 objectn2; // increments the main()s n2// n3; // compile error
}int main()
{int n1 1, n2 2, n3 3;
/*
std::bind 的第二个参数以后都是传递给第一个参数的入参这里的n1就是传值那么f函数里的第一个参数就是值传递第二个参数使用了 std::ref 包裹那么就进行了引用传递那么在 f里面对第二个参数的修改会影响外部函数的局部变量n2
*/std::functionvoid() 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;
}