fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. namespace {
  8.  
  9. // address to number
  10. unsigned a2n(void *addr)
  11. {
  12. static std::vector<void*> addr_list;
  13.  
  14. unsigned i = 0;
  15. for(; i < addr_list.size(); ++i) {
  16. if(addr_list[i] == addr) {
  17. return i;
  18. }
  19. }
  20. addr_list.push_back(addr);
  21. return i;
  22. }
  23.  
  24. // number to string
  25. std::string n2s(unsigned n)
  26. {
  27. switch(n) {
  28. case 0:
  29. return "x";
  30. case 1:
  31. return "tmp";
  32. case 2:
  33. return "y";
  34. default:
  35. return "none";
  36. }
  37. }
  38.  
  39. struct Hoge
  40. {
  41. Hoge()
  42. {
  43. cout << "Hoge : default constructor. (" << n2s(a2n(this)) << ")" << endl;
  44. }
  45.  
  46. ~Hoge()
  47. {
  48. cout << "Hoge : destructor. (" << n2s(a2n(this)) << ")" << endl;
  49. }
  50.  
  51. Hoge(const Hoge& o)
  52. {
  53. cout << "Hoge : copy constructor. (" << n2s(a2n((void*)&o)) << ") -> (" << n2s(a2n(this)) << ")" << endl;
  54. }
  55.  
  56. Hoge(Hoge&& o)
  57. {
  58. cout << "Hoge : move constructor. (" << n2s(a2n((void*)&o)) << ") -> (" << n2s(a2n(this)) << ")" << endl;
  59. }
  60.  
  61. Hoge&& foo()
  62. {
  63. Hoge tmp(*this);
  64. return std::move(tmp);
  65. }
  66. };
  67.  
  68. } // namespace
  69.  
  70. int main()
  71. {
  72. Hoge x;
  73. Hoge y = x.foo();
  74. }
  75.  
Success #stdin #stdout 0s 3064KB
stdin
Standard input is empty
stdout
Hoge : default constructor. (x)
Hoge : copy constructor. (x) -> (tmp)
Hoge : destructor. (tmp)
Hoge : move constructor. (tmp) -> (y)
Hoge : destructor. (y)
Hoge : destructor. (x)