Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. struct Base
  2. {
  3. Base() = default;
  4.  
  5. Base(const Base&)
  6. {
  7. }
  8.  
  9. Base(Base&&)
  10. {
  11. }
  12. };
  13.  
  14. struct Derived : Base
  15. {
  16. };
  17.  
  18. Base foo()
  19. {
  20. Derived derived;
  21. return derived;
  22. }
  23.  
  24. int main()
  25. {
  26. }
  27.  
  28. prog.cc:21:10: warning: local variable 'derived' will be copied despite being returned by name [-Wreturn-std-move]
  29. return derived;
  30. ^~~~~~~
  31. prog.cc:21:10: note: call 'std::move' explicitly to avoid copying
  32. return derived;
  33. ^~~~~~~
  34. std::move(derived)
  35.  
  36. #include <iostream>
  37. #include <iomanip>
  38.  
  39. struct Base
  40. {
  41. bool flag{false};
  42.  
  43. Base()
  44. {
  45. std::cout << "Base construction" << std::endl;
  46. }
  47.  
  48. Base(const bool flag) : flag{flag}
  49. {
  50. }
  51.  
  52. Base(const Base&)
  53. {
  54. std::cout << "Base copy" << std::endl;
  55. }
  56.  
  57. Base(Base&& otherBase)
  58. : flag{otherBase.flag}
  59. {
  60. std::cout << "Base move" << std::endl;
  61. otherBase.flag = false;
  62. }
  63.  
  64. ~Base()
  65. {
  66. std::cout << "Base destruction" << std::endl;
  67. }
  68. };
  69.  
  70. struct Derived : Base
  71. {
  72. Derived()
  73. {
  74. std::cout << "Derived construction" << std::endl;
  75. }
  76.  
  77. Derived(const bool flag) : Base{flag}
  78. {
  79. }
  80.  
  81. Derived(const Derived&):Base()
  82. {
  83. std::cout << "Derived copy" << std::endl;
  84. }
  85.  
  86. Derived(Derived&&)
  87. {
  88. std::cout << "Derived move" << std::endl;
  89. }
  90.  
  91. ~Derived()
  92. {
  93. std::cout << "Derived destruction" << std::endl;
  94. std::cout << "Flag: " << flag << std::endl;
  95. }
  96. };
  97.  
  98. Base foo_copy()
  99. {
  100. std::cout << "foo_copy" << std::endl;
  101. Derived derived{true};
  102. return derived;
  103. }
  104.  
  105. Base foo_move()
  106. {
  107. std::cout << "foo_move" << std::endl;
  108. Derived derived{true};
  109. return std::move(derived);
  110. }
  111.  
  112. int main()
  113. {
  114. std::cout << std::boolalpha;
  115. (void)foo_copy();
  116. std::cout << std::endl;
  117. (void)foo_move();
  118. }
  119.  
  120. foo_copy
  121. Base copy
  122. Derived destruction
  123. Flag: true
  124. Base destruction
  125. Base destruction
  126.  
  127. foo_move
  128. Base move
  129. Derived destruction
  130. Flag: false
  131. Base destruction
  132. Base destruction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement