次の方法で共有


reference_wrapper クラス

参照をラップします。

構文

template <class Ty>
class reference_wrapper
{
    typedef Ty type;

    reference_wrapper(Ty&) noexcept;
    operator Ty&() const noexcept;
    Ty& get() const noexcept;

    template <class... Types>
    auto operator()(Types&&... args) const ->
        decltype(std::invoke(get(), std::forward<Types>(args)...));
};

解説

reference_wrapper<Ty> は、構築可能で、オブジェクトまたは Ty 型の関数への参照の周りの割り当て可能なラッパーのコピーであり、その型のオブジェクトを指すポインターを保持します。 reference_wrapper を使用して参照を標準コンテナーに格納できるほか、std::bind への参照によってオブジェクトを渡すことができます。

Ty 型は、オブジェクトの種類または関数の型である必要があります。それ以外の場合はコンパイル時に静的アサートに失敗します。

ヘルパー関数 std::ref および std::cref は、reference_wrapper オブジェクトの作成に使用できます。

メンバー

コンストラクター

名前 説明
reference_wrapper reference_wrapper を構築します。

Typedefs

名前 説明
result_type ラップされた参照の弱い結果型。
type ラップされた参照の型。

関数

名前 説明
get ラップされた参照を取得します。

演算子

名前 説明
operator Ty& ラップされた参照へのポインターを取得します。
Operator() ラップされた参照を呼び出します。

get

ラップされた参照を取得します。

Ty& get() const noexcept;

解説

このメンバー関数はラップされた参照を返します。

// std__functional__reference_wrapper_get.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int main() {
    int i = 1;
    std::reference_wrapper<int> rwi(i);

    std::cout << "i = " << i << std::endl;
    std::cout << "rwi = " << rwi << std::endl;
    rwi.get() = -1;
    std::cout << "i = " << i << std::endl;

    return (0);
}
i = 1
rwi = 1
i = -1

演算子 Ty&

ラップされた参照を取得します。

operator Ty&() const noexcept;

解説

このメンバー演算子は、 *ptrを返します。

// std__functional__reference_wrapper_operator_cast.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int main() {
    int i = 1;
    std::reference_wrapper<int> rwi(i);

    std::cout << "i = " << i << std::endl;
    std::cout << "(int)rwi = " << (int)rwi << std::endl;

    return (0);
}
i = 1
(int)rwi = 1

Operator()

ラップされた参照を呼び出します。

template <class... Types>
auto operator()(Types&&... args);

パラメーター

types
引数リストの型。

args
引数リスト。

解説

テンプレート メンバー operator()std::invoke(get(), std::forward<Types>(args)...) を返します。

// std__functional__reference_wrapper_operator_call.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val) {
    return (-val);
}

int main() {
    std::reference_wrapper<int (int)> rwi(neg);

    std::cout << "rwi(3) = " << rwi(3) << std::endl;

    return (0);
}
rwi(3) = -3

reference_wrapper

reference_wrapper を構築します。

reference_wrapper(Ty& val) noexcept;

パラメーター

Ty
ラップする型。

val
ラップする値。

解説

このコンストラクターは、ptr の格納された値を &val に設定します。

// std__functional__reference_wrapper_reference_wrapper.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val) {
    return (-val);
}

int main() {
    int i = 1;
    std::reference_wrapper<int> rwi(i);

    std::cout << "i = " << i << std::endl;
    std::cout << "rwi = " << rwi << std::endl;
    rwi.get() = -1;
    std::cout << "i = " << i << std::endl;

    return (0);
}
i = 1
rwi = 1
i = -1

result_type

ラップされた参照の弱い結果型。

typedef R result_type;

解説

result_type typedef は、ラップされた関数の弱い結果型のシノニムです。 この typedef は、関数型に対してのみ意味があります。

// std__functional__reference_wrapper_result_type.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val) {
    return (-val);
}

int main() {
    typedef std::reference_wrapper<int (int)> Mywrapper;
    Mywrapper rwi(neg);
    Mywrapper::result_type val = rwi(3);

    std::cout << "val = " << val << std::endl;

    return (0);
}
val = -3

type

ラップされた参照の型。

typedef Ty type;

解説

この typedef は、テンプレート引数 Ty のシノニムです。

// std__functional__reference_wrapper_type.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>

int neg(int val) {
    return (-val);
}

int main() {
    int i = 1;
    typedef std::reference_wrapper<int> Mywrapper;
    Mywrapper rwi(i);
    Mywrapper::type val = rwi.get();

    std::cout << "i = " << i << std::endl;
    std::cout << "rwi = " << val << std::endl;

    return (0);
}
i = 1
rwi = 1