fork(1) download
  1. #include <memory>
  2.  
  3. template<typename D, typename B>
  4. std::unique_ptr<D> static_cast_ptr(std::unique_ptr<B>& base)
  5. {
  6. return std::unique_ptr<D>(static_cast<D*>(base.release()));
  7. }
  8.  
  9. template<typename D, typename B>
  10. std::unique_ptr<D> static_cast_ptr(std::unique_ptr<B>&& base)
  11. {
  12. return std::unique_ptr<D>(static_cast<D*>(base.release()));
  13. }
  14.  
  15. class Base
  16. {
  17. public:
  18. virtual ~Base() {}
  19. };
  20.  
  21. class Derived : public Base
  22. {
  23. public:
  24. virtual ~Derived() {}
  25. };
  26.  
  27. int main()
  28. {
  29. std::unique_ptr<Base> base_ptr(new Derived);
  30. auto derived_ptr = static_cast_ptr<Derived>(base_ptr);
  31. }
Success #stdin #stdout 0s 3012KB
stdin
Standard input is empty
stdout
Standard output is empty