c++ - 为什么 boost::interprocess::managed_shared_ptr to non-const 不能转换为 managed_shared_ptr to const

标签 c++ boost interprocess

据我了解,以下内容对 boost::shared_ptr 有效:

boost::shared_ptr<SomeData> ptr;
...
boost::shared_ptr<const SomeData> c_ptr = ptr; // Valid

相同的行为不适用于 boost::interprocess::managed_shared_ptr。为什么?

最佳答案

boost::interprocess::managed_shared_ptr实际上不是共享指针;它只是一个辅助类,您可以使用它来定义一个类的类型。来自interprocess docs :

typedef managed_shared_ptr<MyType, managed_shared_memory>::type my_shared_ptr;

And the creation of a shared pointer can be simplified to this:

[c++]

my_shared_ptr sh_ptr = make_managed_shared_ptr (segment.construct<MyType>("object to share")(), segment);

对于上述示例中的“sh_ptr”,以下应该有效:

typedef managed_shared_ptr<const MyType, managed_shared_memory>::type my_shared_const_ptr;
my_shared_const_ptr sh_c_ptr = sh_ptr;

因为这两个对象实际上是共享指针。

另一方面,做:

managed_shared_ptr<MyType, managed_shared_memory> ptr;
managed_shared_ptr<const MyType, managed_shared_memory> c_ptr = ptr;

不会工作,因为在这种情况下 ptr 和 c_ptr 是 very simple structs除了生成 3 个 typedef 之外什么都不做,所以它们不会转换。

关于c++ - 为什么 boost::interprocess::managed_shared_ptr to non-const 不能转换为 managed_shared_ptr to const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12848674/

相关文章:

c++ - 如何为共享内存映射选择固定地址

c++ - boostnamed_mutex私有(private)成员访问错误

c++ - 是否可以非法更改 CLASS 的 PRIVATE 数据成员?

c++ - 更新 CListCtrl 时 MFC 应用程序卡住

c++ - 如果不使用,双端队列会减慢程序速度吗?

c++ - boost 累加器 error_of<mean> 的目的是什么?

c++ - boost-units - 使用无量纲类型的任意系统

c++ - 通过ofstream输出到文件时 boost 序列化 “read access violation”

c++ - boost::进程间消息队列创建时的竞争条件?

c++ - 需要 4 种颜色渐变逻辑