std::function<R(Args...)>::operator()
From cppreference.com
< cpp | utility | functional | function
| R operator()( Args... args ) const; |
(C++11以降) | |
格納された呼び出し可能な関数のターゲットを、パラメータ args を使って呼び出します。
INVOKE<R>(f, std::forward<Args>(args)...) と実質的に同等です。ここで、f は *this の ターゲットオブジェクト です。
目次 |
[edit] Parameters
| args | - | 格納された呼び出し可能な関数のターゲットに渡すパラメータ。 |
[edit] Return value
R が void の場合はなし。それ以外の場合は、格納された呼び出し可能オブジェクトの呼び出しの戻り値。
[edit] Exceptions
std::bad_function_call をスローします。これは、*this が呼び出し可能な関数のターゲットを格納していない場合、つまり !*this == true の場合です。
[edit] Example
次の例は、std::function を値で他の関数に渡す方法を示しています。また、std::function がラムダを格納できることも示しています。
このコードを実行
#include <functional> #include <iostream> 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; try { call(f); } catch (const std::bad_function_call& ex) { std::cout << ex.what() << '\n'; } f = [&n](){ return n; }; call(f); n = 2; call(f); f = normal_function; call(f); std::function<void(std::string, int)> g; g = [](std::string str, int i) { std::cout << str << ' ' << i << '\n'; }; g("Hi", 052); }
実行結果の例
bad_function_call 1 2 42 Hi 42
[edit] See also
| ターゲットを呼び出す ( std::move_only_functionのpublicメンバ関数) | |
| 保存された関数を呼び出す ( std::reference_wrapper<T>のpublicメンバ関数) | |
| (C++11) |
空の std::function を呼び出したときにスローされる例外 (クラス) |
| (C++17)(C++23) |
任意の呼び出し可能 (Callable)オブジェクトを所与の引数で呼び出す (戻り値の型を指定することも可能)(C++23以降) (関数テンプレート) |