fork download
  1.  
  2. #include <iostream>
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. struct Blob {
  8. Blob() : i(0) { cout << "C " << this << endl; }
  9. Blob(const Blob& o) : i(o.i) { cout << "c " << this << " <- " << &o << endl; }
  10. Blob(Blob&& o) : i(o.i) { cout << "m " << this << " <- " << &o << endl; }
  11. Blob& operator =(const Blob&) { cout << "=" << endl; return *this; }
  12. Blob& operator =(Blob&&) { cout << "=m" << endl; return *this; }
  13. ~Blob() { cout << "~ " << this << endl; }
  14.  
  15. int i;
  16. };
  17.  
  18. int main() {
  19. try {
  20. cout << "Throw directly: " << endl;
  21. throw Blob();
  22. } catch(const Blob& b) { cout << "caught: " << &b << endl; }
  23. try {
  24. cout << "Throw with object about to die anyhow" << endl;
  25. Blob b;
  26. throw b;
  27. } catch(const Blob& b) { cout << "caught: " << &b << endl; }
  28. cout << "Throw with object not about to die anyhow (enter non-zero integer)" << endl;
  29. Blob b;
  30. int tmp = 0;
  31. cin >> tmp; //Just trying to keep optimizers from removing dead code
  32. try {
  33. if(tmp) throw b;
  34. cout << "Test is worthless if you enter '0' silly" << endl;
  35. } catch(const Blob& bb) { cout << "caught: " << &bb << endl; }
  36. b.i = tmp;
  37. cout << b.i << endl;
  38. }
Success #stdin #stdout 0s 2968KB
stdin
2
stdout
Throw directly: 
C 0x857f068
caught: 0x857f068
~ 0x857f068
Throw with object about to die anyhow
C 0xbfac4b1c
m 0x857f068 <- 0xbfac4b1c
~ 0xbfac4b1c
caught: 0x857f068
~ 0x857f068
Throw with object not about to die anyhow (enter non-zero integer)
C 0xbfac4b18
m 0x857f068 <- 0xbfac4b18
caught: 0x857f068
~ 0x857f068
2
~ 0xbfac4b18