Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::get_deleter

      From cppreference.com
      <cpp‎ |memory‎ |shared 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)
       
      std::shared_ptr
      Member functions
      Modifiers
      Observers
      (until C++20*)
      Non-member functions
      get_deleter
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      functions(until C++26*)
      Helper classes
      Deduction guides(C++17)
       
      Defined in header<memory>
      template<class Deleter,class T>
      Deleter* get_deleter(conststd::shared_ptr<T>& p)noexcept;
      (since C++11)

      Access to thep's deleter. If the shared pointerp owns a deleter of type cv-unqualifiedDeleter (e.g. if it was created with one of the constructors that take a deleter as a parameter), then returns a pointer to the deleter. Otherwise, returns a null pointer.

      Contents

      [edit]Parameters

      p - a shared pointer whose deleter needs to be accessed

      [edit]Return value

      A pointer to the owned deleter ornullptr. The returned pointer is valid at least as long as there remains at least oneshared_ptr instance that owns it.

      [edit]Notes

      The returned pointer may outlive the lastshared_ptr if, for example,std::weak_ptrs remain and the implementation doesn't destroy the deleter until the entire control block is destroyed.

      [edit]Example

      Demonstrates thatstd::shared_ptr deleter is independent of theshared_ptr's type.

      Run this code
      #include <iostream>#include <memory> struct Foo{int i;};void foo_deleter(Foo* p){std::cout<<"foo_deleter called!\n";    delete p;} int main(){std::shared_ptr<int> aptr; {// create a shared_ptr that owns a Foo and a deleterauto foo_p= new Foo;std::shared_ptr<Foo> r(foo_p, foo_deleter);        aptr=std::shared_ptr<int>(r,&r->i);// aliasing ctor// aptr is now pointing to an int, but managing the whole Foo}// r gets destroyed (deleter not called) // obtain pointer to the deleter:if(auto del_p= std::get_deleter<void(*)(Foo*)>(aptr)){std::cout<<"shared_ptr<int> owns a deleter\n";if(*del_p== foo_deleter)std::cout<<"...and it equals &foo_deleter\n";}elsestd::cout<<"The deleter of shared_ptr<int> is null!\n";}// deleter called here

      Output:

      shared_ptr<int> owns a deleter...and it equals &foo_deleterfoo_deleter called!

      [edit]See also

      std::shared_ptr constructors
      (public member function)
      returns the deleter that is used for destruction of the managed object
      (public member function ofstd::unique_ptr<T,Deleter>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/shared_ptr/get_deleter&oldid=160208"

      [8]ページ先頭

      ©2009-2025 Movatter.jp