Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::move_only_function::operator()

      From cppreference.com
      <cpp‎ |utility‎ |functional‎ |move only function
       
       
      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++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*/);
      (since C++23)

      Invokes the stored callable target with the parametersargs. The/*cv*/,/*ref*/, and/*noex*/ parts ofoperator() are identical to those of the template parameter ofstd::move_only_function.

      Equivalent toreturnstd::invoke_r<R>(/*cv-ref-cast*/(f),std::forward<Args>(args)...);, wheref is a cv-unqualified lvalue that denotes the target object of*this, and/*cv-ref-cast*/(f) is equivalent to:

      • f ifcvref is either empty or&, or
      • std::as_const(f) ifcvref is eitherconst orconst&, or
      • std::move(f) ifcvref is&&, or
      • std::move(std::as_const(f)) ifcvref isconst&&.

      The behavior is undefined if*this is empty.

      Contents

      [edit]Parameters

      args - parameters to pass to the stored callable target

      [edit]Return value

      std::invoke_r<R>(/*cv-ref-cast*/(f),std::forward<Args>(args)...).

      [edit]Exceptions

      Propagates the exception thrown by the underlying function call.

      [edit]Example

      The following example shows howstd::move_only_function can passed to other functions by value. Also, it shows howstd::move_only_function can store lambdas.

      Run this code
      #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(){return42;} 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));}

      Output:

      1242

      [edit]See also

      invokes the target
      (public member function ofstd::function<R(Args...)>)[edit]
      calls the stored function
      (public member function ofstd::reference_wrapper<T>)[edit]
      (C++17)(C++23)
      invokes anyCallable object with given argumentsand possibility to specify return type(since C++23)
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/functional/move_only_function/operator()&oldid=134372"

      [8]ページ先頭

      ©2009-2025 Movatter.jp