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. return a.n.back() > 77 ? a : b;
  20. }
  21.  
  22. int main()
  23. {
  24. A b,c;
  25.  
  26. A a1(b); //Erwartet: normaler Konstruktor. Passt.
  27. A a2(std::move(c)); //Erwartet: Rvalue Construktor: Passt
  28. A a3(f()); //Erwartet: Rvalue Konstruktor. Passt nicht.
  29. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
kopiert
swaped
kopiert