Guest User

Untitled

a guest
Apr 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include <memory>
  2. #include <vector>
  3.  
  4. struct A {
  5. A(std::unique_ptr<int> a) : a(std::move(a)) {}
  6.  
  7. bool gone() {
  8. return a == nullptr;
  9. }
  10. std::unique_ptr<int> a;
  11. };
  12.  
  13. std::vector<A> vec;
  14.  
  15. void foo(A&& a, bool queue) {
  16. if (queue) {
  17. vec.push_back(std::move(a));
  18. }
  19. }
  20.  
  21. int main()
  22. {
  23. A a(std::make_unique<int>(1337));
  24.  
  25. foo(std::move(a), false);
  26. std::printf("Lost ownership: %un", a.gone());
  27. if (a.a != nullptr) {
  28. std::printf("A.a: %dn", *a.a);
  29. }
  30.  
  31.  
  32. foo(std::move(a), true);
  33. std::printf("Lost ownership: %un", a.gone());
  34. if (a.a != nullptr) {
  35. std::printf("A.a: %dn", *a.a);
  36. }
  37.  
  38. }
Add Comment
Please, Sign In to add comment