Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      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)
       
       
      ~unique_ptr();
      (since C++11)
      (constexpr since C++23)

      Ifget()==nullptr there are no effects. Otherwise, the owned object is destroyed viaget_deleter()(get()).

      Requires thatget_deleter()(get()) does not throw exceptions.

      [edit]Notes

      Althoughstd::unique_ptr<T> with the default deleter may be constructed withincomplete typeT, the typeT must be complete at the point of code where the destructor is called.

      [edit]Example

      The following program demonstrates usage of a custom deleter.

      Run this code
      #include <iostream>#include <memory> int main(){auto deleter=[](int* ptr){std::cout<<"[deleter called]\n";        delete ptr;}; std::unique_ptr<int, decltype(deleter)> uniq(newint, deleter);std::cout<<(uniq?"not empty\n":"empty\n");    uniq.reset();std::cout<<(uniq?"not empty\n":"empty\n");}

      Output:

      not empty[deleter called]empty
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/unique_ptr/%7Eunique_ptr&oldid=160467"

      [8]ページ先頭

      ©2009-2026 Movatter.jp