Namespaces
Variants

std::move_only_function:: operator()

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* )
R operator ( ) ( Args... args ) /*cv*/ /*ref*/ noexcept ( /*noex*/ ) ;
(C++23以降)

格納された呼び出し可能なターゲットをパラメータ args で呼び出します。 /*cv*/ /*ref*/ および /*noex*/ の部分は、 operator ( ) のテンプレートパラメータのものと同一です。

return std:: invoke_r < R > ( /*cv-ref-cast*/ ( f ) , std:: forward < Args > ( args ) ... ) ; と等価。ここで f * this のターゲットオブジェクトを表すCV修飾されていない左辺値であり、 /*cv-ref-cast*/ ( f ) は以下と等価:

  • f cv 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以降)
(関数テンプレート)