std::function<R(Args...)>::operator()
提供: cppreference.com
< cpp | utility | functional | function
R operator()( Args... args ) const; |
(C++11以上) | |
格納されている callable な関数ターゲットを、引数 args
で呼び出します。
実質的に INVOKE<R>(f, std::forward<Args>(args)...) を行います。 ただし f
は *this
のターゲットオブジェクト、 INVOKE
は Callable で説明されている操作です。
目次 |
[編集] 引数
args | - | 格納されている callable な関数ターゲットに渡す引数 |
[編集] 戻り値
R
が void の場合は何も返しません。 そうでなければ、格納されている callable なオブジェクトを呼び出した戻り値を返します。
[編集] 例外
- *this が callable な関数ターゲットを格納していない、すなわち !*this == true の場合、 std::bad_function_call を投げます。
[編集] 例
以下の例は、どのように std::function を別の関数に値渡しできるかを示します。 また、どのように std::function にラムダを格納できるかも示します。
Run this code
#include <iostream> #include <functional> void call(std::function<int()> f) // can be passed by value { std::cout << f() << '\n'; } int normal_function() { return 42; } int main() { int n = 1; std::function<int()> f = [&n](){ return n; }; call(f); n = 2; call(f); f = normal_function; call(f); }
出力:
1 2 42
[編集] 関連項目
格納されている関数を呼びます ( std::reference_wrapper<T> のパブリックメンバ関数)
| |
(C++11) |
空の std::function を呼び出したときに投げられる例外 (クラス) |
(C++17) |
任意の Callable なオブジェクトを指定された引数で呼びます (関数テンプレート) |