std::reference_wrapper
來自 cppreference.com
定義於標頭檔案 <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 中的引用型別 (類模板) |