When I write a class like this static_cast()
calls the custom conversion operator. But static_pointer_cast()
does not compile. Why is that, and what do I need to do to make it work?
class A{
//class implementation
operator int(){
return 42;
}
}
A a();
int i = static_cast<int>(a); //works i = 42
std::shared_ptr<A> pa = std::make_shared<A>();
std::shared_ptr<int> pi = static_pointer_cast<int>(pa); //does not compile
Edit: Ultimately I need to convert smart_ptr of templated class, where the templated argument is a derived class, e.g.:
template<typename T>
class Foo;
class Base;
class Derived: public Base{}
shared_ptr<Foo<Derived>> fb = make_shared<Foo<Derived>>();
shared_ptr<Foo<Base>> x = static_pointer_cast<Foo<Base>>(fb);