Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::static_pointer_cast,std::dynamic_pointer_cast,std::const_pointer_cast,std::reinterpret_pointer_cast

      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
      static_pointer_castdynamic_pointer_castconst_pointer_castreinterpret_pointer_cast
      (C++17)
      (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 T,class U>
      std::shared_ptr<T> static_pointer_cast(conststd::shared_ptr<U>& r)noexcept;
      (1)(since C++11)
      template<class T,class U>
      std::shared_ptr<T> static_pointer_cast(std::shared_ptr<U>&& r)noexcept;
      (2)(since C++20)
      template<class T,class U>
      std::shared_ptr<T> dynamic_pointer_cast(conststd::shared_ptr<U>& r)noexcept;
      (3)(since C++11)
      template<class T,class U>
      std::shared_ptr<T> dynamic_pointer_cast(std::shared_ptr<U>&& r)noexcept;
      (4)(since C++20)
      template<class T,class U>
      std::shared_ptr<T> const_pointer_cast(conststd::shared_ptr<U>& r)noexcept;
      (5)(since C++11)
      template<class T,class U>
      std::shared_ptr<T> const_pointer_cast(std::shared_ptr<U>&& r)noexcept;
      (6)(since C++20)
      template<class T,class U>
      std::shared_ptr<T> reinterpret_pointer_cast(conststd::shared_ptr<U>& r)noexcept;
      (7)(since C++17)
      template<class T,class U>
      std::shared_ptr<T> reinterpret_pointer_cast(std::shared_ptr<U>&& r)noexcept;
      (8)(since C++20)

      Creates a new instance ofstd::shared_ptr whose stored pointer is obtained fromr's stored pointer using a cast expression.

      Ifr is empty, so is the newshared_ptr (but its stored pointer is not necessarily null). Otherwise, the newshared_ptr will share ownership with the initial value ofr, except that it is empty if thedynamic_cast performed bydynamic_pointer_cast returns a null pointer.

      LetY betypenamestd::shared_ptr<T>::element_type, then the resultingstd::shared_ptr's stored pointer will be obtained by evaluating, respectively:

      1,2)static_cast<Y*>(r.get())
      3,4)dynamic_cast<Y*>(r.get()). If the result of thedynamic_cast is a null pointer value, the returnedshared_ptr will be empty.
      5,6)const_cast<Y*>(r.get())
      7,8)reinterpret_cast<Y*>(r.get())

      The behavior of these functions is undefined unless the corresponding cast fromU* toT* is well formed:

      1,2) The behavior is undefined unlessstatic_cast<T*>((U*)nullptr) is well formed.
      3,4) The behavior is undefined unlessdynamic_cast<T*>((U*)nullptr) is well formed.
      5,6) The behavior is undefined unlessconst_cast<T*>((U*)nullptr) is well formed.
      7,8) The behavior is undefined unlessreinterpret_cast<T*>((U*)nullptr) is well formed.

      After calling the rvalue overloads(2,4,6,8),r is empty andr.get()== nullptr, except thatr is not modified fordynamic_pointer_cast(4) if thedynamic_cast fails.

      (since C++20)

      Contents

      [edit]Parameters

      r - the pointer to convert

      [edit]Notes

      The expressionsstd::shared_ptr<T>(static_cast<T*>(r.get())),std::shared_ptr<T>(dynamic_cast<T*>(r.get())) andstd::shared_ptr<T>(const_cast<T*>(r.get())) might seem to have the same effect, but they all will likely result in undefined behavior, attempting to delete the same object twice!

      [edit]Possible implementation

      static_pointer_cast
      template<class T,class U>std::shared_ptr<T> static_pointer_cast(conststd::shared_ptr<U>& r)noexcept{auto p=static_cast<typenamestd::shared_ptr<T>::element_type*>(r.get());returnstd::shared_ptr<T>{r, p};}
      dynamic_pointer_cast
      template<class T,class U>std::shared_ptr<T> dynamic_pointer_cast(conststd::shared_ptr<U>& r)noexcept{if(auto p=dynamic_cast<typenamestd::shared_ptr<T>::element_type*>(r.get()))returnstd::shared_ptr<T>{r, p};elsereturnstd::shared_ptr<T>{};}
      const_pointer_cast
      template<class T,class U>std::shared_ptr<T> const_pointer_cast(conststd::shared_ptr<U>& r)noexcept{auto p=const_cast<typenamestd::shared_ptr<T>::element_type*>(r.get());returnstd::shared_ptr<T>{r, p};}
      reinterpret_pointer_cast
      template<class T,class U>std::shared_ptr<T> reinterpret_pointer_cast(conststd::shared_ptr<U>& r)noexcept{auto p=reinterpret_cast<typenamestd::shared_ptr<T>::element_type*>(r.get());returnstd::shared_ptr<T>{r, p};}

      [edit]Example

      Run this code
      #include <iostream>#include <memory> class Base{public:int a;virtualvoid f()const{std::cout<<"I am base!\n";}virtual ~Base(){}}; class Derived:public Base{public:void f()const override{std::cout<<"I am derived!\n";}    ~Derived(){}}; int main(){auto basePtr=std::make_shared<Base>();std::cout<<"Base pointer says: ";    basePtr->f(); auto derivedPtr=std::make_shared<Derived>();std::cout<<"Derived pointer says: ";    derivedPtr->f(); // static_pointer_cast to go up class hierarchy    basePtr= std::static_pointer_cast<Base>(derivedPtr);std::cout<<"Base pointer to derived says: ";    basePtr->f(); // dynamic_pointer_cast to go down/across class hierarchyauto downcastedPtr= std::dynamic_pointer_cast<Derived>(basePtr);if(downcastedPtr){std::cout<<"Downcasted pointer says: ";        downcastedPtr->f();} // All pointers to derived share ownershipstd::cout<<"Pointers to underlying derived: "<< derivedPtr.use_count()<<'\n';}

      Output:

      Base pointer says: I am base!Derived pointer says: I am derived!Base pointer to derived says: I am derived!Downcasted pointer says: I am derived!Pointers to underlying derived: 3

      [edit]See also

      constructs newshared_ptr
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/shared_ptr/pointer_cast&oldid=160268"

      [8]ページ先頭

      ©2009-2025 Movatter.jp