std::reference_wrapper
From cppreference.com
< cpp | utility | functional
| ヘッダ <functional> で定義 |
||
| template< class T > class reference_wrapper; |
(C++11以降) | |
std::reference_wrapperは、参照をコピー可能で代入可能なオブジェクトでラップするクラステンプレートです。
具体的には、std::reference_wrapperは、型Tのオブジェクトへの参照または関数への参照をラップするCopyConstructibleおよびCopyAssignableラッパーです。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以降) |
目次 |
[編集] メンバ型
| type | 定義 |
type
|
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オブジェクトに参照を格納します。 (public member function) | |
| std::reference_wrapperを再バインドします。 (public member function) | |
| 格納された参照にアクセスします。 (public member function) | |
| 保存された関数を呼び出す (public member function) |
[編集] 非メンバ関数
| (C++26) |
reference_wrapperオブジェクトを格納された参照として比較します。(function) |
[編集] 推論ガイド(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) |
1つ以上の引数を関数オブジェクトに束縛する (関数テンプレート) |
| (C++20)(C++20) |
std::reference_wrapperにラップされた参照型を取得します。 (クラステンプレート) |