名前空間
変種
操作

std::reference_wrapper

From cppreference.com
< cpp‎ | utility‎ | functional
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
関数オブジェクト
関数の呼び出し
(C++17)(C++23)
恒等関数オブジェクト
(C++20)
参照ラッパー
reference_wrapper
(C++11)
(C++11)(C++11)
透過的な演算子ラッパー
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

古いバインダとアダプタ
(C++17まで*)
(C++17まで*)
(C++17まで*)
(C++17まで*)  
(C++17まで*)
(C++17*まで)(C++17*まで)(C++17*まで)(C++17*まで)
(C++20まで*)
(C++20まで*)
(C++17*まで)(C++17*まで)
(C++17*まで)(C++17*まで)

(C++17まで*)
(C++17*まで)(C++17*まで)(C++17*まで)(C++17*まで)
(C++20まで*)
(C++20まで*)
 
 
ヘッダ <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::bindstd::threadのコンストラクタ、またはヘルパー関数std::make_pairstd::make_tupleにオブジェクトを参照渡しするために使用されます。また、通常参照を保持できない標準コンテナ(std::vectorなど)内に参照を格納するメカニズムとしても使用できます。

std::reference_wrapperTriviallyCopyableであることが保証されています。

(C++17以降)

Tは不完全型である可能性があります。

(C++20以降)

目次

[編集] メンバ型

type 定義
type T
result_type
(C++17で非推奨)
(C++20で削除)
Tが関数である場合のTの戻り値型。それ以外の場合、定義されません。
argument_type
(C++17で非推奨)
(C++20で削除)
  • Tが型A1の引数を1つ取る関数または関数ポインタである場合、argument_typeA1です。
  • TがクラスT0のメンバ関数へのポインタであり、引数を取らない場合、argument_typeT0*(cv修飾子が付いている可能性あり)です。
  • Tがメンバ型T::argument_typeを持つクラス型である場合、argument_typeはそのエイリアスです。
first_argument_type
(C++17で非推奨)
(C++20で削除)
  • Tが型A1A2の2つの引数を取る関数または関数ポインタである場合、first_argument_typeA1です。
  • TがクラスT0のメンバ関数へのポインタであり、引数を1つ取る場合、first_argument_typeT0*(cv修飾子が付いている可能性あり)です。
  • Tがメンバ型T::first_argument_typeを持つクラス型である場合、first_argument_typeはそのエイリアスです。
second_argument_type
(C++17で非推奨)
(C++20で削除)
  • Tが型A1A2の2つの引数を取る関数または関数ポインタである場合、second_argument_typeA2です。
  • TがクラスT0のメンバ関数へのポインタであり、引数A1を1つ取る場合、second_argument_typeA1(cv修飾子が付いている可能性あり)です。
  • Tがメンバ型T::second_argument_typeを持つクラス型である場合、second_argument_typeはそのエイリアスです。

[編集] メンバ関数

新しいstd::reference_wrapperオブジェクトに参照を格納します。
(public member function) [編集]
std::reference_wrapperを再バインドします。
(public member function) [編集]
格納された参照にアクセスします。
(public member function) [編集]
保存された関数を呼び出す
(public member function) [編集]

[編集] 非メンバ関数

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つ以上の引数を関数オブジェクトに束縛する
(関数テンプレート) [編集]
std::reference_wrapperにラップされた参照型を取得します。
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)