名前空間
変種
操作

std::function

From cppreference.com
< cpp‎ | utility‎ | functional
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
関数オブジェクト
関数の呼び出し
(C++17)(C++23)
恒等関数オブジェクト
(C++20)
透過的な演算子ラッパー
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

古いバインダとアダプタ
(C++17まで*)
(C++17まで*)
(C++17まで*)
(C++17まで*)  
(C++17まで*)
(C++17*まで)(C++17*まで)(C++17*まで)(C++17*まで)
(C++20まで*)
(C++20まで*)
(C++17*まで)(C++17*まで)
(C++17*まで)(C++17*まで)

(C++17まで*)
(C++17*まで)(C++17*まで)(C++17*まで)(C++17*まで)
(C++20まで*)
(C++20まで*)
 
 
ヘッダ <functional> で定義
template< class >
class function; /* 未定義 */
(C++11以降)
template< class R, class... Args >
class function<R(Args...)>;
(C++11以降)

クラステンプレートstd::functionは、汎用的な多態的な関数ラッパーです。std::functionのインスタンスは、ポインタを介した関数、ラムダ式bind式、またはその他の関数オブジェクト、さらにメンバ関数へのポインタやデータメンバへのポインタなど、あらゆるCopyConstructibleCallableターゲットを格納、コピー、呼び出しすることができます。

格納された呼び出し可能オブジェクトは、std::functionターゲットと呼ばれます。std::functionがターゲットを含まない場合、それはと呼ばれます。空のstd::functionターゲットを呼び出すと、std::bad_function_call例外がスローされます。

std::functionCopyConstructibleおよびCopyAssignableの要件を満たします。

目次

[編集] メンバ型

定義
result_type R
argument_type
(C++17で非推奨)(C++20で削除)
sizeof...(Args)==1で、TArgs...の最初で唯一の型である場合のT
first_argument_type
(C++17で非推奨)(C++20で削除)
sizeof...(Args)==2で、T1Args...の2つの型のうち最初のものである場合のT1
second_argument_type
(C++17で非推奨)(C++20で削除)
sizeof...(Args)==2で、T2Args...の2つの型のうち2番目のものである場合のT2

[編集] メンバ関数

新しいstd::functionインスタンスを構築する
(public メンバ関数) [編集]
std::functionインスタンスを破棄する
(public メンバ関数) [編集]
新しいターゲットを代入する
(public メンバ関数) [編集]
内容を交換する
(public メンバ関数) [編集]
(C++17で削除)
新しいターゲットを代入する
(public メンバ関数) [編集]
ターゲットが格納されているかチェックする
(public メンバ関数) [編集]
ターゲットを呼び出す
(public メンバ関数) [編集]
ターゲットアクセス
格納されたターゲットのtypeidを取得する
(public メンバ関数) [編集]
格納されたターゲットへのポインタを取得する
(public メンバ関数) [編集]

[編集] 非メンバ関数

std::swap アルゴリズムを特殊化する
(関数テンプレート) [編集]
(C++20で削除)
std::functionnullptrを比較する
(関数テンプレート) [編集]

[編集] ヘルパークラス

std::uses_allocator 型特性を特殊化する
(クラステンプレート特殊化) [編集]

[編集] 推論ガイド(C++17以降)

[編集] 備考

結果型が参照であるstd::functionを、末尾戻り値型を持たないラムダ式で初期化する場合、注意が必要です。auto推論の仕組みにより、そのようなラムダ式は常にprvalueを返します。したがって、結果として得られる参照は通常、std::function::operator()が戻るときに寿命が終わる一時オブジェクトにバインドされます。

(C++23まで)

参照を返すstd::functionが、prvalueを返す関数または関数オブジェクト(末尾戻り値型を持たないラムダ式を含む)で初期化された場合、返された参照を一時オブジェクトにバインドすることは禁止されているため、プログラムは不正な形式となります。

(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;

[編集] 関連項目

与えられた呼び出しシグネチャで修飾子をサポートする任意の呼び出し可能オブジェクトのムーブ専用ラッパー
(クラステンプレート) [編集]
所与の呼び出しシグネチャにおける修飾子をサポートする、任意のコピー構築可能な呼び出し可能オブジェクトのコピー可能なラッパー
(クラステンプレート) [編集]
任意の呼び出し可能オブジェクトの所有権を持たないラッパー
(クラステンプレート) [編集]
空のstd::functionを呼び出したときにスローされる例外
(クラス) [編集]
(C++11)
メンバへのポインタから関数オブジェクトを生成する
(関数テンプレート) [編集]
typeid 型の情報を問い合わせ、型を表すstd::type_infoオブジェクトを返す
English 日本語 中文(简体) 中文(繁體)