std::function
From cppreference.com
< cpp | utility | functional
| ヘッダ <functional> で定義 |
||
| template< class > class function; /* 未定義 */ |
(C++11以降) | |
| template< class R, class... Args > class function<R(Args...)>; |
(C++11以降) | |
クラステンプレートstd::functionは、汎用的な多態的な関数ラッパーです。std::functionのインスタンスは、ポインタを介した関数、ラムダ式、bind式、またはその他の関数オブジェクト、さらにメンバ関数へのポインタやデータメンバへのポインタなど、あらゆるCopyConstructibleなCallableなターゲットを格納、コピー、呼び出しすることができます。
格納された呼び出し可能オブジェクトは、std::functionのターゲットと呼ばれます。std::functionがターゲットを含まない場合、それは空と呼ばれます。空のstd::functionのターゲットを呼び出すと、std::bad_function_call例外がスローされます。
std::functionはCopyConstructibleおよびCopyAssignableの要件を満たします。
目次 |
[編集] メンバ型
| 型 | 定義 |
result_type
|
R
|
argument_type(C++17で非推奨)(C++20で削除) |
sizeof...(Args)==1で、TがArgs...の最初で唯一の型である場合のT |
first_argument_type(C++17で非推奨)(C++20で削除) |
sizeof...(Args)==2で、T1がArgs...の2つの型のうち最初のものである場合のT1 |
second_argument_type(C++17で非推奨)(C++20で削除) |
sizeof...(Args)==2で、T2がArgs...の2つの型のうち2番目のものである場合のT2 |
[編集] メンバ関数
新しいstd::functionインスタンスを構築する(public メンバ関数) | |
std::functionインスタンスを破棄する(public メンバ関数) | |
| 新しいターゲットを代入する (public メンバ関数) | |
| 内容を交換する (public メンバ関数) | |
| (C++17で削除) |
新しいターゲットを代入する (public メンバ関数) |
| ターゲットが格納されているかチェックする (public メンバ関数) | |
| ターゲットを呼び出す (public メンバ関数) | |
ターゲットアクセス | |
| 格納されたターゲットのtypeidを取得する (public メンバ関数) | |
| 格納されたターゲットへのポインタを取得する (public メンバ関数) | |
[編集] 非メンバ関数
| (C++11) |
std::swap アルゴリズムを特殊化する (関数テンプレート) |
| (C++20で削除) |
std::functionとnullptrを比較する (関数テンプレート) |
[編集] ヘルパークラス
| (C++11) (C++17まで) |
std::uses_allocator 型特性を特殊化する (クラステンプレート特殊化) |
[編集] 推論ガイド(C++17以降)
[編集] 備考
|
結果型が参照である |
(C++23まで) |
|
参照を返す |
(C++23から) |
std::function<const int&()> F([] { return 42; }); // Error since C++23: can't bind // the returned reference to a temporary int x = F(); // Undefined behavior until C++23: the result of F() is a dangling reference std::function<int&()> G([]() -> int& { static int i{0x2A}; return i; }); // OK std::function<const int&()> H([i{052}] -> const int& { return i; }); // OK
[編集] 例
このコードを実行
#include <functional> #include <iostream> struct Foo { Foo(int num) : num_(num) {} void print_add(int i) const { std::cout << num_ + i << '\n'; } int num_; }; void print_num(int i) { std::cout << i << '\n'; } struct PrintNum { void operator()(int i) const { std::cout << i << '\n'; } }; int main() { // store a free function std::function<void(int)> f_display = print_num; f_display(-9); // store a lambda std::function<void()> f_display_42 = []() { print_num(42); }; f_display_42(); // store the result of a call to std::bind std::function<void()> f_display_31337 = std::bind(print_num, 31337); f_display_31337(); // store a call to a member function std::function<void(const Foo&, int)> f_add_display = &Foo::print_add; const Foo foo(314159); f_add_display(foo, 1); f_add_display(314159, 1); // store a call to a data member accessor std::function<int(Foo const&)> f_num = &Foo::num_; std::cout << "num_: " << f_num(foo) << '\n'; // store a call to a member function and object using std::placeholders::_1; std::function<void(int)> f_add_display2 = std::bind(&Foo::print_add, foo, _1); f_add_display2(2); // store a call to a member function and object ptr std::function<void(int)> f_add_display3 = std::bind(&Foo::print_add, &foo, _1); f_add_display3(3); // store a call to a function object std::function<void(int)> f_display_obj = PrintNum(); f_display_obj(18); auto factorial = [](int n) { // store a lambda object to emulate "recursive lambda"; aware of extra overhead std::function<int(int)> fac = [&](int n) { return (n < 2) ? 1 : n * fac(n - 1); }; // note that "auto fac = [&](int n) {...};" does not work in recursive calls return fac(n); }; for (int i{5}; i != 8; ++i) std::cout << i << "! = " << factorial(i) << "; "; std::cout << '\n'; }
実行結果の例
-9 42 31337 314160 314160 num_: 314159 314161 314162 18 5! = 120; 6! = 720; 7! = 5040;
[編集] 関連項目
| (C++23) |
与えられた呼び出しシグネチャで修飾子をサポートする任意の呼び出し可能オブジェクトのムーブ専用ラッパー (クラステンプレート) |
| (C++26) |
所与の呼び出しシグネチャにおける修飾子をサポートする、任意のコピー構築可能な呼び出し可能オブジェクトのコピー可能なラッパー (クラステンプレート) |
| (C++26) |
任意の呼び出し可能オブジェクトの所有権を持たないラッパー (クラステンプレート) |
| (C++11) |
空のstd::functionを呼び出したときにスローされる例外 (クラス) |
| (C++11) |
メンバへのポインタから関数オブジェクトを生成する (関数テンプレート) |
| typeid | 型の情報を問い合わせ、型を表すstd::type_infoオブジェクトを返す |