std::reference_wrapper<T>::operator()
From cppreference.com
< cpp | utility | functional | reference wrapper
template< class... ArgTypes > typename std::result_of<T&(ArgTypes&&...)>::type |
(C++11以降) (C++17まで) |
|
| template< class... ArgTypes > std::invoke_result_t<T&, ArgTypes...> |
(C++17以降) (C++20 以降 constexpr) |
|
保持されている参照が指すCallableオブジェクトを、INVOKE(get(), std::forward<ArgTypes>(args)...)によるかのように呼び出します。 この関数は、保持されている参照がCallableオブジェクトを指している場合にのみ利用可能です。
T は完全型である必要があります。
目次 |
[edit] パラメータ
| args | - | 呼び出される関数に渡される引数 |
[edit] 戻り値
呼び出された関数の戻り値。
[edit] 例外
|
実装定義の例外をスローする場合があります。 |
(C++11以降) (C++17まで) |
|
noexcept 指定:
noexcept(std::is_nothrow_invocable_v<T&, ArgTypes...>) |
(C++17以降) |
[edit] 例
このコードを実行
#include <functional> #include <iostream> void f1() { std::cout << "reference to function called\n"; } void f2(int n) { std::cout << "bind expression called with " << n << " as the argument\n"; } int main() { std::reference_wrapper<void()> ref1 = std::ref(f1); ref1(); auto b = std::bind(f2, std::placeholders::_1); auto ref2 = std::ref(b); ref2(7); auto c = []{ std::cout << "lambda function called\n"; }; auto ref3 = std::ref(c); ref3(); }
出力
reference to function called bind expression called with 7 as the argument lambda function called
[edit] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 3764 | C++17 | operator() は noexcept ではありません。 |
noexcept を伝播します。 |
[edit] 関連項目
| 保持している参照にアクセスします。 (public member function) |