RazzorFlame RazzorFlame - 1 year ago 108
C++ Question

Initialize std::shared_ptr<T> from std::shared_ptr<void>

going quickly to the problem:
I run into it when trying to "copy" from std::shared_ptr< void > to templated std::shared_ptr< T >. It looks like this:

template < >
class TDynamic< void >
std::shared_ptr< void > m_pointer;
// Here i got constructors, functions etc
template < typename U >
TDynamic< U > SwitchType()
TDynamic< U > returnValue;
returnValue.m_pointer = std::shared_ptr< U >(m_pointer); // error here

The error says this:

c:\...path_here...\tdynamic.hpp(426): error C2440: 'type cast': cannot convert from 'const std::shared_ptr< void >' to 'std::shared_ptr< T >'
1> with
1> [
1> T=grim::Actor
1> ]
1> c:\...path_here...\tdynamic.hpp(426): note: No constructor could take the source type, or constructor overload resolution was ambiguous

Compiler & IDE: Visual Studio 2015 Community

This is necessary to me for this situation. Lets say I have class "Actor" and derieved class "APlayer"

class Actor
class APlayer
: public Actor

int main()
{ // some code here
TDynamic< APlayer > test1(new APlayer);
TDynamic< void > test2(test1);
TDynamic< Actor > test3(test2); // error here

I could paste the whole code here but its like 550 lines and most of it does not matter. I just need to copy that std::shared_ptr.

Thanks :)

Answer Source

You can use static_pointer_cast:

returnValue.m_pointer = static_pointer_cast<U>(m_pointer);

It's up to you to guarantee that this cast is legal (it's defined to be as legal as static_cast<U*>(m_pointer.get())).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download