fork download
  1. #include <iostream>
  2. #include <utility> //für std::move
  3. #include <vector>
  4. #include <cstdlib>
  5. struct A
  6. {
  7. std::vector<int> n;
  8.  
  9. A(A&& a) { n.swap(a.n); std::cout<<"swaped"<<std::endl; }
  10. A(const A& a) : n(a.n) { std::cout<<"kopiert"<<std::endl; }
  11. A() {}
  12. };
  13.  
  14. A f() //erstellt irgendein Objekt vom Typ A
  15. {
  16. A a, b;
  17. a.n.push_back(rand());
  18. b.n.push_back(rand());
  19. if (a.n.back() > 77)
  20. return a;
  21. return b;
  22. }
  23.  
  24. int main()
  25. {
  26. A b,c;
  27.  
  28. A a1(b); //Erwartet: normaler Konstruktor. Passt.
  29. A a2(std::move(c)); //Erwartet: Rvalue Construktor: Passt
  30. A a3(f()); //Erwartet: Rvalue Konstruktor. Passt nicht.
  31. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
kopiert
swaped
swaped