4 ms·
const_pointer_cast should do the job.
by rq1 7y ago
const_pointer_cast should do the job.
- gpderetta 7y agoThat's for the reverse (unsafe) conversion. A shared ptr can always be implicitly converted to a shared ptr to const. In fact a shared_ptr<T> can always be implicitly converted to a shared_ptr<T2> as long as T* is convertible to T2*. So for example derived to base and anything to void are allowed.
- mark-r 7y agoDo they then share the reference count, or can you get in trouble by going out of scope in the wrong order?
- gpderetta 7y agoThey will share the same reference count. In fact you can make two shared_ptrs to arbitrary types share the same reference count (although in this case there is no implicit conversion and you need to use the explicit aliasing constructor); it is useful for interior pointers for example.