fork(1) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. using namespace std;
  5.  
  6. struct NTC
  7. {
  8. int x;
  9.  
  10. NTC(int mX) : x(mX) { }
  11.  
  12. ~NTC()
  13. {
  14. cout << "boop." << x << endl;
  15. }
  16. };
  17.  
  18.  
  19.  
  20. int main()
  21. {
  22. using AS = aligned_storage_t<sizeof(NTC), alignof(NTC)>;
  23.  
  24. AS as1, as2;
  25.  
  26. new (&as1) NTC{2};
  27. new (&as2) NTC{5};
  28.  
  29. NTC& in1{*static_cast<NTC*>(static_cast<void*>(&as1))};
  30. NTC& in2{*static_cast<NTC*>(static_cast<void*>(&as2))};
  31.  
  32. std::swap(in1, in2);
  33.  
  34. in1.~NTC();
  35. in2.~NTC();
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
boop.2
boop.5
boop.2