fork download
  1.  
  2.  
  3. #include <iostream>
  4.  
  5. struct base
  6. {
  7. base() { std::cout << "ctor" << std::endl; }
  8. base( const base& ) { std::cout << "copy ctor" << std::endl; }
  9. base& operator = ( const base& ) { std::cout << "copy op" << std::endl; }
  10. base( base&& right ) noexcept { std::cout << "move ctor" << std::endl; }
  11. base& operator = ( base&& right ) noexcept { std::cout << "move op" << std::endl; }
  12. ~base() { std::cout << "dtor" << std::endl; }
  13. };
  14.  
  15.  
  16. namespace A
  17. {
  18. struct sth : base {};
  19. }
  20.  
  21. namespace B
  22. {
  23. struct sth : base
  24. {
  25. sth() {}
  26. sth( const sth& ) {}
  27. sth& operator = ( const sth& ) {}
  28. sth( sth&& ) {}
  29. sth& operator = ( sth&& ) {}
  30. ~sth() {}
  31. };
  32. }
  33.  
  34. namespace C
  35. {
  36. struct sth : base
  37. {
  38. sth() {}
  39. sth( const sth& right ) : base( right ) {}
  40. sth& operator = ( const sth& right )
  41. {
  42. // copy base's members
  43. static_cast < base& >( *this ) = right;
  44.  
  45. /* copy sth's members */
  46. }
  47. sth( sth&& right ) : base( std::move( right ) ) { /* right is no longer available! */ }
  48. sth& operator = ( sth&& right )
  49. {
  50. // move base's members
  51. static_cast < base& >( *this ) = std::move( right );
  52.  
  53. /* move sth's members
  54.   * but now right is not available any longer! */
  55. }
  56. ~sth() {}
  57. };
  58. }
  59.  
  60.  
  61. //
  62. // main
  63. //
  64. int main()
  65. {
  66. std::cout << "case A:" << std::endl;
  67. { // case A
  68. A::sth s1;
  69. A::sth s2 = s1;
  70. A::sth s3 = std::move( s1 );
  71. s1 = s3;
  72. }
  73.  
  74. std::cout << "case B:" << std::endl;
  75. { // case B
  76. B::sth s1;
  77. B::sth s2 = s1;
  78. B::sth s3 = std::move( s1 );
  79. s1 = s3;
  80. }
  81.  
  82. std::cout << "case C:" << std::endl;
  83. { // case C
  84. C::sth s1;
  85. C::sth s2 = s1;
  86. C::sth s3 = std::move( s1 );
  87. s1 = s3;
  88. }
  89. }
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
case A:
ctor
copy ctor
move ctor
copy op
dtor
dtor
dtor
case B:
ctor
ctor
ctor
dtor
dtor
dtor
case C:
ctor
copy ctor
move ctor
copy op
dtor
dtor
dtor