fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. int main()
  5. {
  6. {// I Move semantics
  7. std::shared_ptr<int> sptr;
  8. sptr = std::make_unique<int>(5);
  9. std::cout << *sptr << '\n';
  10. }
  11. {// II The same thing
  12. std::shared_ptr<int> sptr;
  13. std::unique_ptr<int> uptr = std::make_unique<int>(5);
  14. sptr = std::move(uptr);
  15. std::cout << *sptr << ", " << bool(uptr) << '\n';
  16. }
  17. {// III Assignment operator is not allowed
  18. // std::shared_ptr<int> sptr;
  19. // std::unique_ptr<int> uptr = std::make_unique<int>(5);
  20. // sptr = uptr;
  21. // std::cout << *sptr << ", " << bool(uptr) << '\n';
  22. }
  23. {// IV Copy ctor is not allowed
  24. // std::unique_ptr<int> uptr = std::make_unique<int>(5);
  25. // std::shared_ptr<int> sptr = uptr;
  26. // std::cout << *sptr << ", " << bool(uptr) << '\n';
  27. }
  28. }
Success #stdin #stdout 0s 4316KB
stdin
Standard input is empty
stdout
5
5, 0