On 8/10/2015 09:27, Karolin Varner wrote:
On 10/07/2015 09:45 PM, Peter Dimov wrote:
template
unique_ptr<T> dynamic_pointer_cast( unique_ptr<U> && r ) noexcept { T * p = dynamic_cast ( r.get() ); if( p ) r.release(); return unique_ptr<T>( p ); } [...] On 10/07/2015 09:03 AM, Gavin Lambert wrote: On 7/10/2015 19:16, Vicente J. Botet Escriba wrote: Just wondering if we can not make dynamic_pointer_cast to work as your dynamic_moving_pointer_cast, when the parameter is a rvalue reference
It would theoretically be possible if you have access to the internals of shared_ptr (otherwise it would be no better than copying).
We do have access to the internals of the boost smart pointers, so wecould provide a set of specialized moving casts for std::unique_ptr, boost::shared_ptr and boost::scoped_ptr.
unique_ptr is straightforward, as Peter Dimov has demonstrated above. scoped_ptr is similarly straightforward. As I mentioned on the other thread branch though, I'm a bit skeptical that either of these types would be useful for a dynamic moving cast -- dynamic cast yes, moving yes, but both at once is very peculiar semantics. It's shared_ptr that you need internal access for, if you want to be able to avoid the refcount changes completely. And this one is more likely to be useful for dynamic moving casts, though it's still hard to think of a motivating case.
For std::shared_ptr and the other pointers we might still emulate a moving cast in order to facilitate generic programming.
I'm not sure emulation is any better than existing dynamic_pointer_cast;
this was my attempt:
template