std::move_only_function::operator()
R operator()( Args... args ) /*cv*/ /*ref*/ noexcept(/*noex*/);
|
(C++23以上) | |
格納された呼び出し可能なターゲットを引数 args で呼び出します。/*cv*/、/*ref*/、/*noex*/ の各指定子は、operator() のテンプレートパラメータのものと同一です。
std::move_only_function相当します。
以下と同等です: return std::invoke_r<R>(/*cv-ref-cast*/(f), std::forward<Args>(args)...); (ただし、f は *this のターゲットオブジェクトを表す cv 修飾なしの左辺値であり、/*cv-ref-cast*/(f) は以下に相当します):
fcv ref が空または&の場合、またはstd::as_const(f)cv ref がconstまたはconst &の場合、またはstd::move(f)cv ref が&&の場合、またはstd::move(std::as_const(f))cv ref がconst &&の場合。
*this が空の場合、動作は未定義です。
パラメータ
| args | - | 格納された呼び出し可能ターゲットに渡すパラメータ |
戻り値
std:: invoke_r < R > ( /*cv-ref-cast*/ ( f ) , std:: forward < Args > ( args ) ... ) .
例外
基になる関数呼び出しによってスローされた例外を伝播します。
例
以下の例は、 std::move_only_function が値渡しで他の関数に渡せる方法を示しています。また、 std::move_only_function がラムダを格納する方法も示しています。
#include <iostream> #include <functional> void call(std::move_only_function<int() const> f) // can be passed by value { std::cout << f() << '\n'; } int normal_function() { return 42; } int main() { int n = 1; auto lambda = [&n](){ return n; }; std::move_only_function<int() const> f = lambda; call(std::move(f)); n = 2; call(lambda); f = normal_function; call(std::move(f)); }
出力:
1 2 42
関連項目
|
ターゲットを呼び出す
(
std::function<R(Args...)>
の公開メンバ関数)
|
|
|
格納された関数を呼び出す
(
std::reference_wrapper<T>
の公開メンバ関数)
|
|
|
(C++17)
(C++23)
|
任意の
Callable
オブジェクトを指定された引数で呼び出す
(戻り値の型を指定可能)
(C++23以降)
(関数テンプレート) |