fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct my_struct {
  5. int i;
  6.  
  7. my_struct()
  8. : my_struct(0)
  9. {
  10. }
  11.  
  12. my_struct(int i)
  13. : i(i)
  14. {
  15. std::cout << "constructed: " << i << std::endl;
  16. }
  17.  
  18. my_struct(my_struct&& obj) noexcept
  19. : i(obj.i)
  20. {
  21. std::cout << "moved: " << i << std::endl;
  22. }
  23.  
  24. my_struct(const my_struct& obj) noexcept
  25. : i(obj.i)
  26. {
  27. std::cout << "copied: " << i << std::endl;
  28. }
  29.  
  30. my_struct& operator =(const my_struct& obj) {
  31. i = obj.i;
  32. std::cout << "assigned: " << i << std::endl;
  33. return *this;
  34. }
  35. };
  36.  
  37. int main() {
  38. std::vector<my_struct> v{1,2,3};
  39. std::cout << "prepare to resize" << std::endl;
  40. v.resize(32, my_struct(9));
  41. }
  42.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
constructed: 1
constructed: 2
constructed: 3
copied: 1
copied: 2
copied: 3
prepare to resize
constructed: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
copied: 9
moved: 1
moved: 2
moved: 3