Namespaces
Variants

std::function<R(Args...)>:: function

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
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,

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 を様々なソースから構築します。

1,2) 空の empty std::function を作成します。
3) other ターゲット * this のターゲットにコピーします。
otherが空の場合、 other が空であれば、この呼び出し直後に * this も空になります。
4) other のターゲットを * this のターゲットに移動します。
otherが空の場合、 other が空であれば、この呼び出し直後に * this も空になります。
other この呼び出し直後、有効だが未規定の状態になります。
5) ターゲットを std:: forward < F > ( f ) で初期化します。ターゲットの型は std:: decay < F > :: type です。
fが関数へのヌルポインタ、メンバーへのヌルポインタ、または何らかの 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以降)
F CopyConstructible でない場合、動作は未定義です。
6-10) (1-5) と同様であるが、 alloc std::function が使用する可能性のある内部データ構造用のメモリ割り当てに使用される点が異なる。

ターゲットが関数ポインタまたは std::reference_wrapper である場合、small object optimizationが保証されます。つまり、これらのターゲットは常に直接 std::function オブジェクト内に格納され、動的メモリ確保は発生しません。その他の大きなオブジェクトは、動的に確保されたストレージ内に構築され、 std::function オブジェクトによってポインタを通じてアクセスされる可能性があります。

目次

パラメータ

other - 初期化に使用される関数オブジェクト * this
f - 初期化に使用される呼び出し可能オブジェクト * this
alloc - 内部メモリ割り当てに使用される Allocator
型要件
-
Alloc Allocator の要件を満たさなければならない

例外

3,8,9) other のターゲットが関数ポインタまたは std::reference_wrapper である場合は例外を送出しない。それ以外の場合は、 std::bad_alloc または格納された呼び出し可能オブジェクトのコピー/ムーブコンストラクタによって送出される例外を送出する可能性がある。
4) other のターゲットが関数ポインタまたは std::reference_wrapper である場合は例外を送出しない。それ以外の場合は、 std::bad_alloc または格納された呼び出し可能オブジェクトのコピー/ムーブコンストラクタによって送出される例外を送出する可能性がある。
(until C++20)
5,10) 以下の場合には例外を投げない: f が関数ポインタまたは std::reference_wrapper である場合。それ以外の場合には、 std::bad_alloc または格納された呼び出し可能オブジェクトのコピーコンストラクタによって投げられる任意の例外を投げる可能性がある。

注記

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 の公開メンバ関数)