Why boost :: interprocess :: managed_shared_ptr non-const cannot be converted to managed_shared_ptr to const

As I understand it, the following is valid for boost :: shared_ptr:

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

The same behavior does not work for boost::interprocess::managed_shared_ptr . Why?

+4
source share
1 answer

boost::interprocess::managed_shared_ptr is not really a shared pointer; it's just a helper class that you can use to determine the type of one. From interprocess docs :

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

And creating a shared pointer can be simplified:

[C ++]

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

The following should work with the sh_ptr example from the above example:

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

Because these two objects are common pointers.

On the other hand, doing:

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

will not work, because in this case ptr and c_ptr are very simple structures that do nothing but make 3 typedefs, so they won’t convert.

+1
source

Source: https://habr.com/ru/post/1439257/


All Articles