fork download
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. class Foo
  5. {
  6. public:
  7. Foo(int x) : data_(x)
  8. {
  9. std::cout << " constructing " << std::endl;
  10. }
  11.  
  12. ~Foo()
  13. {
  14. std::cout << " destructing " << std::endl;
  15. }
  16.  
  17. Foo& operator=(const Foo&) = default;
  18. Foo& operator=(Foo&&) = default;
  19.  
  20. Foo(Foo&& other) noexcept : data_(std::move(other.data_))
  21. {
  22. std::cout << " Move constructing " << std::endl;
  23. }
  24.  
  25. Foo(const Foo& other) noexcept : data_(other.data_)
  26. {
  27. std::cout << " Copy constructing " << std::endl;
  28. }
  29.  
  30. private:
  31. int data_;
  32. };
  33.  
  34.  
  35. int main ( int argc, char *argv[])
  36. {
  37. std::vector<Foo> v;
  38. v.reserve(2);
  39. v.emplace_back(1);
  40. std::cout << "Added 1" << std::endl;
  41. v.emplace_back(2);
  42. std::cout << "Added 2" << std::endl;
  43. v.emplace_back(3);
  44. std::cout << "Added 3" << std::endl;
  45. std::cout << "v size: " << v.size() << std::endl;
  46. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
 constructing Added 1 constructing Added 2 constructing Move constructing Move constructing destructing destructing Added 3 v size: 3 destructing destructing destructing