C++ reinterpret_cast 对 std::shared_ptr 引用进行优化

标签 c++ c++11 shared-ptr smart-pointers reinterpret-cast

你有两个类 AnimalDog (其中 Dog 继承自 Animal ),并且您经常遇到一种情况,即您经常期待一只动物,但正在发送一只狗的实例。在我的特殊情况下,我经常将强指针( std::shared_ptr<Dog> )转换到动物期待函数( std::shared_ptr<Animal> )。

如果我们接受我们可以使函数参数成为引用( std::shared_ptr<Animal>& ,避免争论为什么不应该将强指针作为引用参数,因为担心会改变线程的所有权),我认为我们在内存方面是安全的投一个std::shared_ptr<Dog> dog使用 reinterpret_cast<std::shared_ptr<Animal>&>(dog) , 对?

如果是这样,除了线程问题之外还会出现什么问题;比如引用计数品种?

需要明确的是,目的是有一个可以在许多情况下使用的解决方案,在这种情况下,强制转换一次并不是真正可行的解决方案。更多的问题是必须转换许多对象。另外,忽略 std::unique_ptr可能是也可能不是更好的解决方案。

添加最终要求 - 使用普通指针不允许我更改原始 std::shared_ptr在通用序列化程序类函数的情况下,它是虚拟的,因此不能被模板化。

最佳答案

由于无法直接使用static_cast , const_cast , dynamic_castreinterpret_caststd::shared_ptr检索与作为参数传递的指针共享所有权的指针,std::static_pointer_cast , std::const_pointer_cast , std::dynamic_pointer_caststd::reinterpret_pointer_cast应该使用函数。
std::reinterpret_pointer_cast在 C++11 和 C++14 中不可用,因为它仅由 N3920 提出并被纳入图书馆基础 TS in February 2014 .但是,它可以按如下方式实现:

template <typename To, typename From>
inline std::shared_ptr<To> reinterpret_pointer_cast(
    std::shared_ptr<From> const & ptr) noexcept
{ return std::shared_ptr<To>(ptr, reinterpret_cast<To *>(ptr.get())); }

关于C++ reinterpret_cast 对 std::shared_ptr 引用进行优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40336882/

相关文章:

c++ - 在 Visual Studio 中使用自制库

c++ - 有没有办法为 NUMA 中的数据分配特定的内存?

python - Boost.Python boost::shared_ptr 没有通过值转换器找到 python

c++ - 为什么我们不能将 "="用于 shared_ptr 或 unique_ptr?

c++ - vector 超出范围而不给出错误

C++初学者删除代码

c++ - 声明未知维度的二维数组的最佳方法是什么?

c++ - 如何让 "factory function"返回一个不可复制的对象?

c++ - 使用二进制读写连接文件

c++ - 缺少 shared_ptr 和 weak_ptr 之间的相等性