Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      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)
       
       
      pointer release()noexcept;
      (since C++11)
      (constexpr since C++23)

      Releases the ownership of the managed object, if any.

      get() returnsnullptr after the call.

      The caller is responsible for cleaning up the object (e.g. by use ofget_deleter()).

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      Pointer to the managed object ornullptr if there was no managed object, i.e. the value which would be returned byget() before the call.

      [edit]Example

      Run this code
      #include <cassert>#include <iostream>#include <memory> struct Foo{    Foo(){std::cout<<"Foo\n";}    ~Foo(){std::cout<<"~Foo\n";}}; // Ownership of the Foo resource is transferred when calling this functionvoid legacy_api(Foo* owning_foo){std::cout<< __func__<<'\n';// [legacy code that no one understands or dares touch anymore]// [...]    delete owning_foo;} int main(){std::unique_ptr<Foo> managed_foo(new Foo);// [code that might return or throw or some such]// [...]    legacy_api(managed_foo.release()); assert(managed_foo== nullptr);}

      Output:

      Foolegacy_api~Foo

      [edit]See also

      returns a pointer to the managed object
      (public member function)[edit]
      returns the deleter that is used for destruction of the managed object
      (public member function)[edit]
      replaces the managed object
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/unique_ptr/release&oldid=152335"

      [8]ページ先頭

      ©2009-2025 Movatter.jp