#include <memory>
template<typename D, typename B>
std::unique_ptr<D> static_cast_ptr(std::unique_ptr<B>& base)
{
return std::unique_ptr<D>(static_cast<D*>(base.release()));
}
template<typename D, typename B>
std::unique_ptr<D> static_cast_ptr(std::unique_ptr<B>&& base)
{
return std::unique_ptr<D>(static_cast<D*>(base.release()));
}
class Base
{
public:
virtual ~Base() {}
};
class Derived : public Base
{
public:
virtual ~Derived() {}
};
int main()
{
std::unique_ptr<Base> base_ptr(new Derived);
auto derived_ptr = static_cast_ptr<Derived>(base_ptr);
}
I2luY2x1ZGUgPG1lbW9yeT4KCnRlbXBsYXRlPHR5cGVuYW1lIEQsIHR5cGVuYW1lIEI+CnN0ZDo6dW5pcXVlX3B0cjxEPiBzdGF0aWNfY2FzdF9wdHIoc3RkOjp1bmlxdWVfcHRyPEI+JiBiYXNlKQp7CiAgICByZXR1cm4gc3RkOjp1bmlxdWVfcHRyPEQ+KHN0YXRpY19jYXN0PEQqPihiYXNlLnJlbGVhc2UoKSkpOwp9CiAKdGVtcGxhdGU8dHlwZW5hbWUgRCwgdHlwZW5hbWUgQj4Kc3RkOjp1bmlxdWVfcHRyPEQ+IHN0YXRpY19jYXN0X3B0cihzdGQ6OnVuaXF1ZV9wdHI8Qj4mJiBiYXNlKQp7CiAgICByZXR1cm4gc3RkOjp1bmlxdWVfcHRyPEQ+KHN0YXRpY19jYXN0PEQqPihiYXNlLnJlbGVhc2UoKSkpOwp9CgpjbGFzcyBCYXNlCnsKcHVibGljOgogICAgdmlydHVhbCB+QmFzZSgpIHt9Cn07CgpjbGFzcyBEZXJpdmVkIDogcHVibGljIEJhc2UKewpwdWJsaWM6CiAgICB2aXJ0dWFsIH5EZXJpdmVkKCkge30KfTsKCmludCBtYWluKCkKewogICAgc3RkOjp1bmlxdWVfcHRyPEJhc2U+IGJhc2VfcHRyKG5ldyBEZXJpdmVkKTsKICAgIGF1dG8gZGVyaXZlZF9wdHIgPSBzdGF0aWNfY2FzdF9wdHI8RGVyaXZlZD4oYmFzZV9wdHIpOwp9