std::function<R(Args...)>::function
function() noexcept;
|
(1) | (C++11以上) |
function( std::nullptr_t ) noexcept;
|
(2) | (C++11以上) |
function( const function& other );
|
(3) | (C++11以上) |
function( function&& other );
|
(4) | (C++11以上) (C++20まで) |
function( function&& other ) noexcept;
|
(C++20以上) | |
template< class F >
function( F&& f );
|
(5) | (C++11以上) |
template< class Alloc >
function( std::allocator_arg_t, const Alloc& alloc ) noexcept;
|
(6) | (C++11以上) (C++17で削除) |
template< class Alloc >
function( std::allocator_arg_t, const Alloc& alloc,
std::nullptr_t ) noexcept;
|
(7) | (C++11以上) (C++17で削除) |
template< class Alloc >
function( std::allocator_arg_t, const Alloc& alloc,
const function& other );
|
(8) | (C++11以上) (C++17で削除) |
template< class Alloc >
function( std::allocator_arg_t, const Alloc& alloc,
function&& other );
|
(9) | (C++11以上) (C++17で削除) |
template< class F, class Alloc >
function( std::allocator_arg_t, const Alloc& alloc, F f );
|
(10) | (C++11以上) (C++17で削除) |
様々なソースから std::function を構築します。
other は空になります。*this が空の場合、other のターゲットを *this のターゲットにムーブします。other が空の場合、*this の呼び出し後もother は有効ですが未規定の状態になります。std::forward<F>(f) でターゲットを初期化します。ターゲットの型は std::decay<F>::type です。f がヌル関数ポインタ、ヌルメンバポインタ、または何らかの std::function 特殊化の空の値である場合、*this の呼び出し後は空になります。
|
(C++23以上) |
- 型
std::decay<F>::typeの左辺値が、引数型Args...と戻り値型Rに対して呼び出し可能である。
|
|
(C++23以上) |
F が CopyConstructible でない場合、動作は未定義です。alloc が std::function が使用する可能性のある内部データ構造のメモリ割り当てに使用される点が異なります。ターゲットが関数ポインタまたは std::reference_wrapper である場合、小オブジェクト最適化が保証されます。つまり、これらのターゲットは常に std::function オブジェクト内に直接格納され、動的メモリ割り当ては行われません。その他の大きなオブジェクトは動的に割り当てられた記憶域に構築され、std::function オブジェクトからポインタを介してアクセスされる可能性があります。
パラメータ
| other | - | 初期化に使用される関数オブジェクト * this |
| f | - | 初期化に使用される呼び出し可能オブジェクト * this |
| alloc | - | 内部メモリ割り当てに使用される Allocator |
| 型要件 | ||
-
Alloc
は
Allocator
の要件を満たさなければならない
|
||
例外
|
4)
other
のターゲットが関数ポインタまたは
std::reference_wrapper
である場合は例外を送出しない。それ以外の場合は、
std::bad_alloc
または格納された呼び出し可能オブジェクトのコピー/ムーブコンストラクタによって送出される例外を送出する可能性がある。
|
(until C++20) |
注記
std::function
のアロケータサポートは、仕様が不十分で実装も一貫していませんでした。一部の実装ではオーバーロード
(
6-10
)
を全く提供しておらず、一部はオーバーロードを提供しているものの指定されたアロケータ引数を無視し、また一部はオーバーロードを提供し構築時には指定されたアロケータを使用するものの、
std::function
が再代入される際には使用しないという状況でした。この結果、C++17ではアロケータサポートが削除されました。
例
#include <functional> #include <iostream> #include <utility> void print_num(int i) { std::cout << "print_num(" << i << ")\n"; } int main() { std::function<void(int)> func1; // (1) 空コンストラクタ try { func1(333 << 1); } catch (const std::bad_function_call& ex) { std::cout << "1) " << ex.what() << '\n'; } std::function<void(int)> func2{nullptr}; // (2) 空コンストラクタ try { func1(222 * 3); } catch (const std::bad_function_call& ex) { std::cout << "2) " << ex.what() << '\n'; } func1 = print_num; // 代入演算子を使用してfunc1を初期化 std::function<void(int)> func3{func1}; // (3) コピーコンストラクタ func3(33); std::function<void(int)> func4{std::move(func3)}; // (4) ムーブコンストラクタ、 // func3は未規定の状態 func4(44); std::function<void(int)> func5{print_num}; // (5) 関数によるコンストラクタ func5(55); // (5) ラムダによるコンストラクタ std::function<void(int)> func6([](int i) { std::cout << "lambda(" << i << ")\n"; }); func6(66); }
出力例:
1) bad_function_call 2) bad_function_call print_num(33) print_num(44) print_num(55) lambda(66)
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2132 | C++11 | オーバーロード ( 5,10 ) が曖昧になる可能性があった | 制約を追加 |
| LWG 2774 | C++11 | ( 5,10 ) が追加のムーブ操作を実行していた | 排除 |
関連項目
新しい
std::move_only_function
オブジェクトを構築する
(
std::move_only_function
の公開メンバ関数)
|