std::move_only_function::operator()
From cppreference.com
< cpp | utility | functional | move only function
| R operator()( Args... args ) /*cv*/ /*ref*/ noexcept(/*noex*/); |
(C++23から) | |
格納されている呼び出し可能なターゲットをパラメータargsで呼び出します。operator()の/*cv*/、/*ref*/、および/*noex*/の部分は、std::move_only_functionのテンプレートパラメータのものと同一です。
fを*thisのターゲットオブジェクトを示すcv修飾されていないlvalueとし、/*cv-ref-cast*/(f)を次のように定義するとき、return std::invoke_r<R>(/*cv-ref-cast*/(f), std::forward<Args>(args)...);と同等です。
- f (
cvrefが空または&の場合) - std::as_const(f) (
cvrefがconstまたはconst &の場合) - std::move(f) (
cvrefが&&の場合) - std::move(std::as_const(f)) (
cvrefが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...)>のpublicメンバ関数) | |
| 保存された関数を呼び出す ( std::reference_wrapper<T>のpublicメンバ関数) | |
| (C++17)(C++23) |
任意の呼び出し可能 (Callable)オブジェクトを所与の引数で呼び出す (戻り値の型を指定することも可能)(C++23以降) (関数テンプレート) |