名前空間
変種
操作

std::reference_wrapper<T>::get, std::reference_wrapper<T>::operator T&

From cppreference.com
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、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)
透過的な演算子ラッパー
(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まで*)
 
std::reference_wrapper
メンバ関数
reference_wrapper::getreference_wrapper::operator T&
非メンバ関数
(C++26)(C++26)
推論ガイド (C++17)
ヘルパークラス
 
operator T& () const noexcept;
(1) (C++11以降)
(C++20 以降 constexpr)
T& get() const noexcept;
(2) (C++11以降)
(C++20 以降 constexpr)

格納された参照を返します。

目次

[編集] パラメータ

(なし)

[編集] 戻り値

格納された参照。

[編集]

#include <cassert>
#include <functional>
#include <map>
#include <optional>
#include <string_view>
 
using Map = std::map<std::string_view, int>;
using Opt = std::optional<std::reference_wrapper<Map::value_type>>;
 
Opt find(Map& m, std::string_view s)
{
    auto it = m.find(s);
    return it == m.end() ? Opt{} : Opt{*it};
}
 
int main()
{
    Map m{{"A", 1}, {"B", 2}, {"C", 3}};
 
    if (auto opt = find(m, "C"); opt)
        opt->get().second = 42;
        // std::optional::operator->() returns reference to std::reference_wrapper, then
        // reference_wrapper::get() returns reference to map::value_type, i.e. std::pair
 
    assert(m["C"] == 42);
}

[編集] 関連項目

保存された関数を呼び出す
(public member function) [編集]
English 日本語 中文(简体) 中文(繁體)