Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::default_delete

      From cppreference.com
      <cpp‎ |memory
       
       
      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)
       
      Defined in header<memory>
      template<class T>struct default_delete;
      (1)(since C++11)
      template<class T>struct default_delete<T[]>;
      (2)(since C++11)

      std::default_delete is the default destruction policy used bystd::unique_ptr when no deleter is specified. Specializations ofdefault_delete are empty classes on typical implementations, and used in theempty base class optimization.

      1) The non-specializeddefault_delete usesdelete to deallocate memory for a single object.
      2) A partial specialization for array types that usesdelete[] is also provided.

      Contents

      [edit]Member functions

      (constructor)
      constructs adefault_delete object
      (public member function)[edit]
      operator()
      deletes the object or array
      (public member function)[edit]

      std::default_delete::default_delete

      constexpr default_delete()noexcept=default;
      (1)
      Primary template specializations
      template<class U>
      default_delete(const default_delete<U>& d)noexcept;
      (2)(since C++11)
      (constexpr since C++23)
      Array specializations
      template<class U>
      default_delete(const default_delete<U[]>& d)noexcept;
      (3)(since C++11)
      (constexpr since C++23)
      1) Constructs astd::default_delete object.
      2) Constructs astd::default_delete<T> object from anotherstd::default_delete object.
      This overload participates in overload resolution only ifU* is implicitly convertible toT*.
      3) Constructs astd::default_delete<T[]> object from anotherstd::default_delete<U[]> object.
      This overload participates in overload resolution only ifU(*)[] is implicitly convertible toT(*)[].

      Parameters

      d - a deleter to copy from

      Notes

      Theconverting constructor template ofstd::default_delete makes possible the implicit conversion fromstd::unique_ptr<Derived> tostd::unique_ptr<Base>.

      std::default_delete::operator()

      Primary template specializations
      void operator()( T* ptr)const;
      (1)(since C++11)
      (constexpr since C++23)
      Array specializations
      template<class U>
      void operator()( U* ptr)const;
      (2)(since C++11)
      (constexpr since C++23)
      1) Callsdelete onptr.
      2) Callsdelete[] onptr.
      This overload participates in overload resolution only ifU(*)[] is implicitly convertible toT(*)[].
      IfU is an incomplete type, the program is ill-formed.

      Parameters

      ptr - an object or array to delete

      Exceptions

      No exception guarantees.

      [edit]Invoking over Incomplete Types

      At the point in the code theoperator() is called, the type must be complete. In some implementations astatic_assert is used to make sure this is the case. The reason for this requirement is that callingdelete on an incomplete type is undefined behavior in C++ if the complete class type has a nontrivial destructor or a deallocation function, as the compiler has no way of knowing whether such functions exist and must be invoked.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_constexpr_memory202202L(C++23)constexpr constructor andoperator()

      [edit]Example

      Run this code
      #include <algorithm>#include <memory>#include <vector> int main(){//  {//      std::shared_ptr<int> shared_bad(new int[10]);//  } // the destructor calls delete, undefined behavior {std::shared_ptr<int> shared_good(newint[10], std::default_delete<int[]>());}// OK: the destructor calls delete[] {std::unique_ptr<int> ptr(newint(5));}// unique_ptr<int> uses default_delete<int> {std::unique_ptr<int[]> ptr(newint[10]);}// unique_ptr<int[]> uses default_delete<int[]> // default_delete can be used anywhere a delete functor is neededstd::vector<int*> v;for(int n=0; n<100;++n)        v.push_back(newint(n));std::for_each(v.begin(), v.end(), std::default_delete<int>());}

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2118C++11member functions of the array specializations rejected qualification conversionsaccept

      [edit]See also

      (C++11)
      smart pointer with unique object ownership semantics
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/default_delete&oldid=179177"

      [8]ページ先頭

      ©2009-2025 Movatter.jp