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 >
{
protected:
std::shared_ptr< void > m_pointer;
public:
// 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 derived class "APlayer"
class Actor
{
public:
};
class APlayer
: public Actor
{
public:
};
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
.
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())
).