Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::unique_ptr<T,Deleter>::reset

      From cppreference.com
      <cpp‎ |memory‎ |unique ptr
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
       
       
      members of the primary template, unique_ptr<T>
      void reset( pointer ptr= pointer())noexcept;
      (1)(constexpr since C++23)
      members of the specialization unique_ptr<T[]>
      template<class U>
      void reset( U ptr)noexcept;
      (2)(constexpr since C++23)
      void reset(std::nullptr_t= nullptr)noexcept;
      (3)(constexpr since C++23)

      Replaces the managed object.

      1,2) Equivalent toauto old_ptr= get();
      /* assigns “ptr” to the stored pointer */
      if(old_ptr)
          get_deleter()(old_ptr);
      .
      Ifget_deleter()(old_ptr) throws an exception, the behavior is undefined.
      2) This overload participates in overload resolution only ifU is the same type aspointer, or all following conditions are satisfied:
      • pointer is the same type aselement_type*.
      • U is a pointer typeV* such thatV(*)[] is convertible toelement_type(*)[].
      3) Equivalent toreset(pointer()).

      Contents

      [edit]Parameters

      ptr - pointer to a new object to manage

      [edit]Notes

      To replace the managed object while supplying a new deleter as well, move assignment operator may be used.

      A test for self-reset, i.e. whetherptr points to an object already managed by*this, is not performed, except where provided as a compiler extension or as a debugging assert. Note that code such asp.reset(p.release()) does not involve self-reset, only code likep.reset(p.get()) does.

      [edit]Example

      Run this code
      #include <iostream>#include <memory> struct Foo// object to manage{    Foo(){std::cout<<"Foo...\n";}    ~Foo(){std::cout<<"~Foo...\n";}}; struct D// deleter{void operator()(Foo* p){std::cout<<"Calling delete for Foo object...\n";        delete p;}}; int main(){std::cout<<"Creating new Foo...\n";std::unique_ptr<Foo, D> up(new Foo(), D());// up owns the Foo pointer (deleter D) std::cout<<"Replace owned Foo with a new Foo...\n";    up.reset(new Foo());// calls deleter for the old one std::cout<<"Release and delete the owned Foo...\n";    up.reset(nullptr);}

      Output:

      Creating new Foo...Foo...Replace owned Foo with a new Foo...Foo...Calling delete for Foo object...~Foo...Release and delete the owned Foo...Calling delete for Foo object...~Foo...

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2118C++11unique_ptr<T[]>::reset rejected qualification conversionsaccepts
      LWG 2169C++11the overloadunique_ptr<T[]>::reset(pointer) existedremoved the overload

      [edit]See also

      returns a pointer to the managed object and releases the ownership
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/unique_ptr/reset&oldid=177110"

      [8]ページ先頭

      ©2009-2025 Movatter.jp