「cpp/utility/functional/function/operator()」の版間の差分
提供: cppreference.com
< cpp | utility | functional | function
細 (Fix some translations) |
|||
1行: | 1行: | ||
− | |||
{{cpp/utility/functional/function/title | operator()}} | {{cpp/utility/functional/function/title | operator()}} | ||
{{cpp/utility/functional/function/navbar}} | {{cpp/utility/functional/function/navbar}} | ||
− | {{ddcl | | + | {{ddcl | sincec++11 | |
− | R operator()( | + | R operator()( ... args ) const; |
}} | }} | ||
− | + | callable {{tt|args}} | |
− | === | + | |
+ | |||
+ | ====== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | args | | + | {{par | args | callable }} |
{{par end}} | {{par end}} | ||
− | === | + | ====== |
− | + | {{tt|R}} {{c|void}} callable | |
===例外=== | ===例外=== | ||
− | * | + | * {{c|*this}} callable {{c|!*this {{==}} true}} |
===例=== | ===例=== | ||
{{example | {{example | ||
− | | | + | | {{lc|std::function}} {{lc|std::function}} |
| code= | | code= | ||
#include <iostream> | #include <iostream> | ||
54行: | 55行: | ||
}} | }} | ||
− | === | + | ====== |
− | {{ | + | {{ |
+ | |||
+ | |||
+ | |||
+ | }} | ||
[[de:cpp/utility/functional/function/operator()]] | [[de:cpp/utility/functional/function/operator()]] |
2018年3月21日 (水) 06:25時点における版
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 なオブジェクトを指定された引数で呼びます (関数テンプレート) |