std::ref
时间: 2023-06-21 18:10:00 浏览: 65
`std::ref` 是 C++ 标准库中的一个函数模板,用于将一个对象转化为一个可被引用的对象。它接受一个对象的引用作为参数,并返回一个包装了该引用的可被引用对象。这个可被引用对象可以像引用一样使用,并且可以传递给需要引用的函数或算法。
通常情况下,当需要将一个对象作为引用参数传递给函数时,可以直接使用该对象的引用。但是有些情况下,需要将该对象包装成一个可被引用的对象,例如当需要将该对象作为一个算法的参数传递时,或者需要将该对象存储在容器中时。这时就可以使用 `std::ref` 来将该对象包装成一个可被引用的对象。
使用 `std::ref` 的示例如下:
```c++
#include <functional>
#include <iostream>
void func(int& n) {
std::cout << "n = " << n << std::endl;
}
int main() {
int n = 42;
std::ref(n); // 包装 n 的引用,但不会改变 n 的值
func(n); // 直接传递 n 的引用
func(std::ref(n)); // 将 n 包装成可被引用的对象,传递给 func
return 0;
}
```
相关问题
std::ref 的用法
std::ref 是一个模板函数,用于引用传递一个对象而不是拷贝。通常,当我们想要将一个对象传递给一个函数并且可能会修改这个对象时,我们会使用引用传递来避免拷贝。然而,在某些情况下,我们希望将对象作为参数传递给一个函数,但是这个函数需要一个复制对象,我们不能直接传递引用。这就是 std::ref 函数的作用,它可以创建一个可以被复制的对象,但是这个对象可以被引用来修改原始对象。例如:
```
#include <iostream>
#include <functional>
void increment(int& n) {
++n;
}
int main() {
int n = 0;
std::function<void()> f = std::bind(increment, std::ref(n));
f();
std::cout << n << std::endl;
return 0;
}
```
这个例子通过 std::function 和 std::bind 使用了 std::ref 函数来创建了一个可复制的引用对象。当调用 f() 时,它将调用 increment 函数来递增原始对象 n 的值。
std::ref()函数
std::ref()函数主要在函数式编程中使用,比如在std::bind中。std::bind是对参数进行拷贝,无法传入引用类型的参数。为了解决这个问题,引入了std::ref()函数。使用std::ref()可以在模板传参的时候传入引用。这样可以确保参数以引用的方式传递,而不是进行拷贝。在一些情况下,比如多线程std::thread的可调用对象期望入参为引用时,也需要显式使用std::ref()来绑定引用进行传参。需要注意的是,std::ref()只是尝试模拟引用传递,并不能真正变成引用。在非模板情况下,std::ref()无法实现引用传递,只有在模板自动推导类型或类型隐式转换时,std::ref()能够使用包装类型reference_wrapper来代替原本会被识别的值类型,从而实现引用传递。
#### 引用[.reference_title]
- *1* *3* [C++ std::ref————详解](https://blog.csdn.net/m0_51551385/article/details/123965079)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [std::ref用法](https://blog.csdn.net/qq_33726635/article/details/124086352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)