名前空間
変種
操作

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

From cppreference.com
< cpp‎ | utility‎ | functional‎ | function
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、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まで*)
 
 
R operator()( Args... args ) const;
(C++11以降)

格納された呼び出し可能な関数のターゲットを、パラメータ args を使って呼び出します。

INVOKE<R>(f, std::forward<Args>(args)...) と実質的に同等です。ここで、f*thisターゲットオブジェクト です。

目次

[edit] Parameters

args - 格納された呼び出し可能な関数のターゲットに渡すパラメータ。

[edit] Return value

Rvoid の場合はなし。それ以外の場合は、格納された呼び出し可能オブジェクトの呼び出しの戻り値。

[edit] Exceptions

std::bad_function_call をスローします。これは、*this が呼び出し可能な関数のターゲットを格納していない場合、つまり !*this == true の場合です。

[edit] Example

次の例は、std::function を値で他の関数に渡す方法を示しています。また、std::function がラムダを格納できることも示しています。

#include <functional>
#include <iostream>
 
void call(std::function<int()> f) // can be passed by value
{ 
    std::cout << f() << '\n';
}
 
int normal_function()
{
    return 42;
}
 
int main()
{
    int n = 1;
    std::function<int()> f;
    try
    {
        call(f);
    }
    catch (const std::bad_function_call& ex)
    {
        std::cout << ex.what() << '\n';
    }
 
    f = [&n](){ return n; };
    call(f);
 
    n = 2;
    call(f);
 
    f = normal_function;
    call(f);
 
    std::function<void(std::string, int)> g;
    g = [](std::string str, int i) { std::cout << str << ' ' << i << '\n'; };
    g("Hi", 052);
}

実行結果の例

bad_function_call
1
2
42
Hi 42

[edit] See also

ターゲットを呼び出す
std::move_only_functionのpublicメンバ関数) [編集]
保存された関数を呼び出す
std::reference_wrapper<T>のpublicメンバ関数) [編集]
空の std::function を呼び出したときにスローされる例外
(クラス) [編集]
(C++17)(C++23)
任意の呼び出し可能 (Callable)オブジェクトを所与の引数で呼び出す (戻り値の型を指定することも可能)(C++23以降)
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)