fork download
  1. #include <iostream>
  2. #include <utility>
  3. #include <vector>
  4.  
  5. int i = 0;
  6. struct A
  7. {
  8. A() : j( ++i )
  9. {
  10. std::cout<<"constructor "<<j<<std::endl;
  11. }
  12. A( const A & c) : j(c.j)
  13. {
  14. std::cout<<"copy "<<j<<std::endl;
  15. }
  16. A( const A && c) : j(c.j)
  17. {
  18. std::cout<<"move "<<j<<std::endl;
  19. }
  20. ~A()
  21. {
  22. std::cout<<"destructor "<<j<<std::endl;
  23. }
  24.  
  25. int j;
  26. };
  27.  
  28. typedef std::vector< A > vec;
  29.  
  30. void foo( vec & v )
  31. {
  32. v.push_back( std::move( A() ) );
  33. }
  34.  
  35. int main()
  36. {
  37. vec v;
  38. v.reserve(2);
  39.  
  40. foo( v );
  41. foo( v );
  42. }
Success #stdin #stdout 0s 2964KB
stdin
Standard input is empty
stdout
constructor 1
move 1
destructor 1
constructor 2
move 2
destructor 2
destructor 1
destructor 2