Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::move_only_function

      From cppreference.com
      <cpp‎ |utility‎ |functional
       
       
      Utilities library
       
      Function objects
      Function wrappers
      (C++11)
      move_only_function
      (C++23)
      (C++11)
      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*)
       
       
      Defined in header<functional>
      template<class...>
      class move_only_function;// not defined
      (1)(since C++23)
      template<class R,class...Args>

      class move_only_function<R(Args...)>;
      template<class R,class...Args>
      class move_only_function<R(Args...)noexcept>;
      template<class R,class...Args>
      class move_only_function<R(Args...)&>;
      template<class R,class...Args>
      class move_only_function<R(Args...)&noexcept>;
      template<class R,class...Args>
      class move_only_function<R(Args...)&&>;
      template<class R,class...Args>
      class move_only_function<R(Args...)&&noexcept>;
      template<class R,class...Args>
      class move_only_function<R(Args...)const>;
      template<class R,class...Args>
      class move_only_function<R(Args...)constnoexcept>;
      template<class R,class...Args>
      class move_only_function<R(Args...)const&>;
      template<class R,class...Args>
      class move_only_function<R(Args...)const&noexcept>;
      template<class R,class...Args>
      class move_only_function<R(Args...)const&&>;
      template<class R,class...Args>

      class move_only_function<R(Args...)const&&noexcept>;
      (2)(since C++23)

      Class templatestd::move_only_function is a general-purpose polymorphic function wrapper.std::move_only_function objects can store and invoke any constructible (not required to be move constructible)Callabletarget — functions,lambda expressions,bind expressions, or other function objects, as well as pointers to member functions and pointers to member objects.

      The stored callable object is called thetarget ofstd::move_only_function. If astd::move_only_function contains no target, it is calledempty. Unlikestd::function, invoking anemptystd::move_only_function results in undefined behavior.

      std::move_only_functions supports every possible combination ofcv-qualifiers (not includingvolatile),ref-qualifiers, andnoexcept-specifiers provided in its template parameter. These qualifiers and specifier (if any) are added to itsoperator().

      std::move_only_function satisfies the requirements ofMoveConstructible andMoveAssignable, but does not satisfyCopyConstructible orCopyAssignable.

      Contents

      [edit]Member types

      Type Definition
      result_typeR

      [edit]Member functions

      constructs a newstd::move_only_function object
      (public member function)[edit]
      destroys astd::move_only_function object
      (public member function)[edit]
      replaces or destroys the target
      (public member function)[edit]
      swaps the targets of twostd::move_only_function objects
      (public member function)[edit]
      checks if thestd::move_only_function has a target
      (public member function)[edit]
      invokes the target
      (public member function)[edit]

      [edit]Non-member functions

      specializes thestd::swap algorithm
      (function)[edit]
      (C++23)
      compares astd::move_only_function withnullptr
      (function)[edit]

      [edit]Notes

      Implementations may store a callable object of small size within thestd::move_only_function object. Such small object optimization is effectively required for function pointers andstd::reference_wrapper specializations, and can only be applied to typesT for whichstd::is_nothrow_move_constructible_v<T> istrue.

      If astd::move_only_function returning a reference is initialized from a function or function object returning a prvalue (including a lambda expression without a trailing-return-type), the program is ill-formed because binding the returned reference to a temporary object is forbidden. See alsostd::function Notes.

      Feature-test macroValueStdFeature
      __cpp_lib_move_only_function202110L(C++23)std::move_only_function

      [edit]Example

      Run this code
      #include <functional>#include <future>#include <iostream> int main(){std::packaged_task<double()> packaged_task([](){return3.14159;}); std::future<double> future= packaged_task.get_future(); auto lambda=[task= std::move(packaged_task)]() mutable{ task();}; //  std::function<void()> function = std::move(lambda); // Error    std::move_only_function<void()> function= std::move(lambda);// OK     function(); std::cout<< future.get();}

      Output:

      3.14159

      [edit]See also

      (C++11)
      copyable wrapper of any copy constructible callable object
      (class template)[edit]
      non-owning wrapper of any callable object
      (class template)[edit]
      copyable wrapper of any copy constructible callable object that supports qualifiers in a given call signature
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/functional/move_only_function&oldid=178279"

      [8]ページ先頭

      ©2009-2025 Movatter.jp