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以降) |
| (4) | ||
|
function
(
function
&&
other
)
;
|
(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,
|
(7) |
(C++11以降)
(C++17で削除) |
|
template
<
class
Alloc
>
function
(
std::
allocator_arg_t
,
const
Alloc
&
alloc,
|
(8) |
(C++11以降)
(C++17で削除) |
|
template
<
class
Alloc
>
function
(
std::
allocator_arg_t
,
const
Alloc
&
alloc,
|
(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
を様々なソースから構築します。
std::function
特殊化の空の値である場合、
*
this
は呼び出し直後に空になります。
|
(C++23以降) |
-
型
std::
decay
<
F
>
::
type
の左辺値が、引数型
Args...と戻り値型Rに対して呼び出し可能であること。
|
std:: is_copy_constructible_v < std:: decay_t < F >> または std:: is_constructible_v < std:: decay_t < F > , F > が false の場合、プログラムは不適格です。 |
(C++23以降) |
std::function
が使用する可能性のある内部データ構造用のメモリ割り当てに使用される点が異なる。
ターゲットが関数ポインタまたは std::reference_wrapper である場合、small object optimizationが保証されます。つまり、これらのターゲットは常に直接 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
の公開メンバ関数)
|