C++库std::ref
1 介绍
std::ref是一个函数模板,它返回一个std::reference_wrapper对象,这个对象包装了一个引用。std::ref的主要用途是在需要按引用传递参数的地方,而标准库算法和函数对象通常会按值传递参数。
2 语法
template< class T >
std::reference_wrapper<T> ref( T& t ) noexcept;
3 示例
头文件:
#include <functional>
以下示例展示了如何使用 std::ref 包装一个引用并传递给一个函数对象:
#include <iostream>
#include <functional>
void print(int& n) {
std::cout << n << std::endl;
}
int main() {
int x = 10;
std::function<void()> func = std::bind(print, std::ref(x));
func(); // 输出: 10
return 0;
}
4 使用场景
4.1 多线程编程
在多线程编程中,有时需要将引用传递给线程函数。std::ref 在这种情况下非常有用:
#include <iostream>
#include <thread>
#include <functional>
void thread_func(int& n) {
n += 1;
}
int main() {
int x = 10;
std::thread t(thread_func, std::ref(x));
t.join();
std::cout << x << std::endl; // 输出: 11
return 0;
}
4.2 实现回调机制
在回调机制中,可以使用 std::ref 传递引用以便在回调函数中修改外部变量:
#include <iostream>
#include <functional>
void callback(int& n) {
n *= 2;
}
void register_callback(std::function<void(int&)> cb, int& value) {
cb(value);
}
int main() {
int x = 5;
register_callback(callback, std::ref(x));
std::cout << x << std::endl; // 输出: 10
return 0;
}
4.3 标准库算法
当使用标准库算法时,如果希望算法对容器中的元素进行修改,可以使用 std::ref:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
void increment(int& n) {
++n;
}
int main() {
std::vector<int> vec{1, 2, 3, 4, 5};
std::for_each(vec.begin(), vec.end(), std::ref(increment));
for (const auto& elem : vec) {
std::cout << elem << " "; // 输出: 2 3 4 5 6
}
return 0;
}
4.4 绑定函数参数
在使用 std::bind 绑定函数参数时,如果需要绑定一个引用参数,可以使用 std::ref。
复制代码
#include <iostream>
#include <functional>
void update_value(int& n, int new_value) {
n = new_value;
}
int main() {
int x = 5;
auto bound_func = std::bind(update_value, std::ref(x), 10);
bound_func();
std::cout << x << std::endl; // 输出: 10
return 0;
}
5 注意事项
5.1 与直接引用&的区别
直接传引用和std::ref各有优劣。直接传引用简单直观,适用于函数参数传递和性能优化场景,而 std::ref 则提供了更灵活的方式,使得引用能够在标准库算法、函数对象和多线程编程中方便地传递。在实际编程中,选择哪种方式取决于具体的使用场景和需求。
| 特性 | 直接传引用& | std::ref |
|---|---|---|
| 使用场景 | 函数参数、性能优化 | 标准库算法、函数对象、多线程编程 |
| 易用性 | 简单直接 | 需要额外的包装操作 |
| 适用范围 | 仅限函数参数 | 标准库算法、函数对象、回调、多线程编程 |
| 安全性 | 容易理解和控制 | 需要注意引用对象的生命周期 |
| 性能 | 最优 | 有少量的包装开销 |
示例比较:
#include <iostream>
void increment(int& n) {
++n;
}
int main() {
int x = 10;
increment(x);
std::cout << "直接引用:"<< x << std::endl; // 输出: 11
x = 10;
auto func = std::bind(increment, std::ref(x));
func();
std::cout <<"使用std::ref:"<< x << std::endl; // 输出: 11
return 0;
}
5.2 引用生命周期
确保被引用的对象在 std::reference_wrapper 的生命周期内有效。如果对象被销毁,引用将变为悬空引用,导致未定义行为:
#include <iostream>
#include <functional>
int main() {
std::function<void()> func;
{
int x = 10;
func = std::bind([](int& n){ std::cout << n << std::endl; }, std::ref(x));
}
// func(); // 未定义行为,因为 x 已经被销毁
return 0;
}
5.3 与标准库容器的结合
使用std::ref包装的引用不能直接存储在标准库容器中,因为容器存储的是值而不是引用。解决方案是存储std::reference_wrapper,不过这种用法比较少。
#include <iostream>
#include <vector>
#include <functional>
int main() {
int x = 10;
int y = 20;
std::vector<std::reference_wrapper<int>> vec{std::ref(x), std::ref(y)};
for (auto& elem : vec) {
elem.get() += 1;
}
std::cout << x << " " << y << std::endl; // 输出: 11 21
return 0;
}
1502

被折叠的 条评论
为什么被折叠?



