Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::function<R(Args...)>::operator=

      From cppreference.com
      <cpp‎ |utility‎ |functional‎ |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*)
       
       
      function& operator=(const function& other);
      (1)(since C++11)
      function& operator=( function&& other);
      (2)(since C++11)
      function& operator=(std::nullptr_t)noexcept;
      (3)(since C++11)
      template<class F>
      function& operator=( F&& f);
      (4)(since C++11)
      template<class F>
      function& operator=(std::reference_wrapper<F> f)noexcept;
      (5)(since C++11)

      Assigns a newtarget tostd::function.

      1) Assigns a copy oftarget ofother, as if by executingfunction(other).swap(*this);
      2) Moves thetarget ofother to*this.other is in a valid state with an unspecified value.
      3) Drops the currenttarget.*this isempty after the call.
      4) Sets thetarget of*this to the callablef, as if by executingfunction(std::forward<F>(f)).swap(*this);. This operator does not participate in overload resolution unlessf isCallable for argument typesArgs... and return typeR.
      5) Sets thetarget of*this to a copy off, as if by executingfunction(f).swap(*this);

      Contents

      [edit]Parameters

      other - anotherstd::function object to copy the target of
      f - a callable to initialize thetarget with
      Type requirements
      -
      F must meet the requirements ofCallable.

      [edit]Return value

      *this

      [edit]Notes

      Even before allocator support was removed fromstd::function in C++17, these assignment operators use the default allocator rather than the allocator of*this or the allocator ofother (seeLWG issue 2386).

      [edit]Example

      Run this code
      #include <cassert>#include <functional>#include <utility> int inc(int n){return n+1;} int main(){std::function<int(int)> f1;std::function<int(int)> f2(inc);assert(f1== nullptr and f2!= nullptr);     f1= f2;// overload (1)assert(f1!= nullptr and f1(1)==2);     f1= std::move(f2);// overload (2)assert(f1!= nullptr and f1(1)==2);// f2 is in valid but unspecified state     f1= nullptr;// overload (3)assert(f1== nullptr);     f1= inc;// overload (4)assert(f1!= nullptr and f1(1)==2);     f1=[](int n){return n+ n;};// overload (4)assert(f1!= nullptr and f1(2)==4); std::reference_wrapper<int(int)> ref1=std::ref(inc);    f1= ref1;// overload (5)assert(f1!= nullptr and f1(1)==2);}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2132C++11the overload(4) taking aCallable object might be ambiguousconstrained
      LWG 2401C++11assignment operator(3) fromstd::nullptr_t not required to be noexceptrequired

      [edit]See also

      replaces or destroys the target
      (public member function ofstd::move_only_function)[edit]
      (removed in C++17)
      assigns a new target
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/functional/function/operator%3D&oldid=176869"

      [8]ページ先頭

      ©2009-2025 Movatter.jp