std::reference_wrapper
来自 cppreference.cn
< cpp | utility | functional
定义于头文件 <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 中的引用类型(类模板) |