std::static_pointer_cast, std::dynamic_pointer_cast, std::const_pointer_cast, std::reinterpret_pointer_cast
来自 cppreference.com
在头文件 <memory> 中定义 |
||
template< class T, class U > std::shared_ptr<T> static_pointer_cast( const std::shared_ptr<U>& r ) noexcept; |
(1) | (自 C++11 起) |
template< class T, class U > std::shared_ptr<T> static_pointer_cast( std::shared_ptr<U>&& r ) noexcept; |
(2) | (自 C++20 起) |
template< class T, class U > std::shared_ptr<T> dynamic_pointer_cast( const std::shared_ptr<U>& r ) noexcept; |
(3) | (自 C++11 起) |
template< class T, class U > std::shared_ptr<T> dynamic_pointer_cast( std::shared_ptr<U>&& r ) noexcept; |
(4) | (自 C++20 起) |
template< class T, class U > std::shared_ptr<T> const_pointer_cast( const std::shared_ptr<U>& r ) noexcept; |
(5) | (自 C++11 起) |
template< class T, class U > std::shared_ptr<T> const_pointer_cast( std::shared_ptr<U>&& r ) noexcept; |
(6) | (自 C++20 起) |
template< class T, class U > std::shared_ptr<T> reinterpret_pointer_cast( const std::shared_ptr<U>& r ) noexcept; |
(7) | (自 C++17 起) |
template< class T, class U > std::shared_ptr<T> reinterpret_pointer_cast( std::shared_ptr<U>&& r ) noexcept; |
(8) | (自 C++20 起) |
创建一个新的 std::shared_ptr 实例,其存储的指针是从 r 的存储指针使用强制转换表达式获得的。
如果 r 为空,则新的 shared_ptr
也是空的(但其存储指针不一定是空指针)。否则,新的 shared_ptr
将与 r 的初始值共享所有权,除非 dynamic_cast
由 dynamic_pointer_cast
执行返回空指针。
令 Y
为 typename std::shared_ptr<T>::element_type,则生成的 std::shared_ptr 的存储指针将分别通过求值获得
1,2) static_cast<Y*>(r.get())
3,4) dynamic_cast<Y*>(r.get())。如果
dynamic_cast
的结果为空指针值,则返回的 shared_ptr
将为空。5,6) const_cast<Y*>(r.get())
7,8) reinterpret_cast<Y*>(r.get())
除非从 U*
到 T*
的相应强制转换形式良好,否则这些函数的行为未定义
1,2) 除非 static_cast<T*>((U*)nullptr) 形式良好,否则行为未定义。
3,4) 除非 dynamic_cast<T*>((U*)nullptr) 形式良好,否则行为未定义。
5,6) 除非 const_cast<T*>((U*)nullptr) 形式良好,否则行为未定义。
7,8) 除非 reinterpret_cast<T*>((U*)nullptr) 形式良好,否则行为未定义。
在调用右值重载 (2,4,6,8) 之后,r 为空,并且 r.get() == nullptr,除非对于 |
(自 C++20 起) |
内容 |
[编辑] 参数
r | - | 要转换的指针 |
[编辑] 说明
表达式 std::shared_ptr<T>(static_cast<T*>(r.get())),std::shared_ptr<T>(dynamic_cast<T*>(r.get())) 和 std::shared_ptr<T>(const_cast<T*>(r.get())) 似乎具有相同的效果,但它们都可能导致未定义的行为,试图两次删除同一个对象!
[编辑] 可能的实现
static_pointer_cast |
---|
template<class T, class U> std::shared_ptr<T> static_pointer_cast(const std::shared_ptr<U>& r) noexcept { auto p = static_cast<typename std::shared_ptr<T>::element_type*>(r.get()); return std::shared_ptr<T>{r, p}; } |
dynamic_pointer_cast |
template<class T, class U> std::shared_ptr<T> dynamic_pointer_cast(const std::shared_ptr<U>& r) noexcept { if (auto p = dynamic_cast<typename std::shared_ptr<T>::element_type*>(r.get())) return std::shared_ptr<T>{r, p}; else return std::shared_ptr<T>{}; } |
const_pointer_cast |
template<class T, class U> std::shared_ptr<T> const_pointer_cast(const std::shared_ptr<U>& r) noexcept { auto p = const_cast<typename std::shared_ptr<T>::element_type*>(r.get()); return std::shared_ptr<T>{r, p}; } |
reinterpret_pointer_cast |
template<class T, class U> std::shared_ptr<T> reinterpret_pointer_cast(const std::shared_ptr<U>& r) noexcept { auto p = reinterpret_cast<typename std::shared_ptr<T>::element_type*>(r.get()); return std::shared_ptr<T>{r, p}; } |
[编辑] 示例
运行此代码
#include <iostream> #include <memory> class Base { public: int a; virtual void 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 hierarchy auto downcastedPtr = std::dynamic_pointer_cast<Derived>(basePtr); if (downcastedPtr) { std::cout << "Downcasted pointer says: "; downcastedPtr->f(); } // All pointers to derived share ownership std::cout << "Pointers to underlying derived: " << derivedPtr.use_count() << '\n'; }
输出
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
[编辑] 另请参阅
构造新的 shared_ptr (公有成员函数) |