std::reference_wrapper
来自 cppreference.cn
定义于头文件 <functional> |
||
template< class T > class reference_wrapper; |
(C++11 起) | |
std::reference_wrapper
是一个类模板,它将一个引用包装成一个可拷贝、可赋值的对象。
具体来说,std::reference_wrapper
是一个 可拷贝构造 和 可拷贝赋值 的包装器,用于包装类型为 T
的对象引用或函数引用。std::reference_wrapper
的实例是对象(它们可以被拷贝或存储在容器中),但它们可以隐式转换为 T&,因此它们可以用作接受底层类型引用的函数的参数。
如果存储的引用是 可调用 的,那么 std::reference_wrapper
可以用相同的参数进行调用。
辅助函数 std::ref 和 std::cref 经常用于生成 std::reference_wrapper
对象。
std::reference_wrapper
用于将对象通过引用传递给 std::bind、std::thread 的构造函数,或者辅助函数 std::make_pair 和 std::make_tuple。它也可以用作在标准容器(如 std::vector)中存储引用的一种机制,因为标准容器通常不能直接存储引用。
|
(C++17 起) |
|
(C++20 起) |
目录 |
[编辑] 成员类型
类型 | 定义 |
类型
|
T
|
result_type (C++17 中已弃用) (C++20 中移除) |
如果 T 是函数,则为 T 的返回类型。否则,未定义。 |
argument_type (C++17 中已弃用) (C++20 中移除) |
|
first_argument_type (C++17 中已弃用) (C++20 中移除) |
|
second_argument_type (C++17 中已弃用) (C++20 中移除) |
|
[编辑] 成员函数
在新 std::reference_wrapper 对象中存储一个引用 (公有成员函数) | |
重新绑定 std::reference_wrapper (公有成员函数) | |
访问存储的引用 (公有成员函数) | |
调用存储的函数 (公有成员函数) |
[编辑] 非成员函数
(C++26) |
将 reference_wrapper 对象与其存储的引用进行比较(函数) |
[编辑] 推导指引(C++17 起)
[编辑] 辅助类
确定 reference_wrapper 和非 reference_wrapper 的共同引用类型(类模板特化) |
[编辑] 可能实现
namespace detail { template<class T> constexpr T& FUN(T& t) noexcept { return t; } template<class T> void FUN(T&&) = delete; } template<class T> class reference_wrapper { public: // types using type = T; // construct/copy/destroy template<class U, class = decltype( detail::FUN<T>(std::declval<U>()), std::enable_if_t<!std::is_same_v<reference_wrapper, std::remove_cvref_t<U>>>() )> constexpr reference_wrapper(U&& u) noexcept(noexcept(detail::FUN<T>(std::forward<U>(u)))) : _ptr(std::addressof(detail::FUN<T>(std::forward<U>(u)))) {} reference_wrapper(const reference_wrapper&) noexcept = default; // assignment reference_wrapper& operator=(const reference_wrapper& x) noexcept = default; // access constexpr operator T& () const noexcept { return *_ptr; } constexpr T& get() const noexcept { return *_ptr; } template<class... ArgTypes> constexpr std::invoke_result_t<T&, ArgTypes...> operator() (ArgTypes&&... args ) const noexcept(std::is_nothrow_invocable_v<T&, ArgTypes...>) { return std::invoke(get(), std::forward<ArgTypes>(args)...); } private: T* _ptr; }; // deduction guides template<class T> reference_wrapper(T&) -> reference_wrapper<T>; |
[编辑] 示例
演示 std::reference_wrapper
作为引用容器的使用,使其能够通过多个索引访问相同的容器。
运行此代码
#include <algorithm> #include <functional> #include <iostream> #include <list> #include <numeric> #include <random> #include <vector> void println(auto const rem, std::ranges::range auto const& v) { for (std::cout << rem; auto const& e : v) std::cout << e << ' '; std::cout << '\n'; } int main() { std::list<int> l(10); std::iota(l.begin(), l.end(), -4); // can't use shuffle on a list (requires random access), but can use it on a vector std::vector<std::reference_wrapper<int>> v(l.begin(), l.end()); std::ranges::shuffle(v, std::mt19937{std::random_device{}()}); println("Contents of the list: ", l); println("Contents of the list, as seen through a shuffled vector: ", v); std::cout << "Doubling the values in the initial list...\n"; std::ranges::for_each(l, [](int& i) { i *= 2; }); println("Contents of the list, as seen through a shuffled vector: ", v); }
可能的输出
Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5 Contents of the list, as seen through a shuffled vector: -1 2 -2 1 5 0 3 -3 -4 4 Doubling the values in the initial list... Contents of the list, as seen through a shuffled vector: -2 4 -4 2 10 0 6 -6 -8 8
[编辑] 另请参见
(C++11)(C++11) |
创建一个类型从其参数推导的 std::reference_wrapper (函数模板) |
(C++11) |
将一个或多个参数绑定到函数对象 (函数模板) |
(C++20)(C++20) |
获取包装在 std::reference_wrapper 中的引用类型 (类模板) |