fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct S {
  5. S() { cout << this << "->S()" << endl; }
  6. // куча конструкторов, чтобы убедиться, что мы нечаянно не сделали копию
  7. // было бы достаточно объявить их как =delete
  8. S(S&& t) { cout << this << "->S(&& " << &t << ")" << endl; }
  9. S(const S& t) { cout << this << "->S(const& " << &t << ")" << endl; }
  10. ~S() { cout << this << "->~S()" << endl; }
  11. };
  12.  
  13. // обман компилятора, чтобы он не ругался на взятие адреса у временного объекта
  14. S* addr(const S& t) { return const_cast<S*>(&t); }
  15.  
  16. S* f(S* p = addr(S())) {
  17. cout << "f(" << p << ")" << endl;
  18. return p;
  19. }
  20.  
  21. int x(int v) {
  22. cout << "x(" << v << ")" << endl;
  23. return v;
  24. }
  25.  
  26. void g(int a, S& t, int b) {
  27. cout << "g(" << a << ", &" << &t << ", " << b << ")" << endl;
  28. }
  29.  
  30. int main() {
  31. cout << "--- begin ---" << endl;
  32. g(x(1), *f(), x(2));
  33. cout << "--- end ---" << endl;
  34. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
--- begin ---
x(2)
0x7fff6b8ed64f->S()
f(0x7fff6b8ed64f)
x(1)
g(1, &0x7fff6b8ed64f, 2)
0x7fff6b8ed64f->~S()
--- end ---