fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct test {
  7. int const value;
  8. test (int v) : value (v) {
  9. cout << "construct " << this << " (" << value << ")"<< endl;
  10. }
  11. test (test const & other) : value (other. value){
  12. cout << "copy construct " << this << " (" << value << ")"<< " from " << (& other) << " (" << value << ")"<< endl;
  13. }
  14. ~test () {
  15. cout << "destruct " << this << " (" << value << ")" << endl;
  16. }
  17. };
  18.  
  19.  
  20.  
  21. template<typename T>
  22. union ac {
  23. T thing;
  24. ac & operator=(ac<T> const & other) {
  25. thing. ~T();
  26. new (& thing) T(other. thing);
  27. }
  28. ac(T && t) : thing (std:: forward<T>(t))
  29. {}
  30. ac(ac<T> const & other) : thing (other. thing) {}
  31. ~ac() {
  32. thing. ~T();
  33. }
  34. };
  35.  
  36.  
  37. int main() {
  38. cout << "-------------" << endl;
  39. ac<test> one {test {1}};
  40. cout << "-------------" << endl;
  41. ac<test> two {test {2}};
  42. cout << "-------------" << endl;
  43. ac<test> also_one {one};
  44. cout << "-------------" << endl;
  45. also_one = two;
  46. cout << "-------------" << endl;
  47. cout << " a vector with them" << endl;
  48. vector<ac<test>> v {test {3}, one, two};
  49. cout << "gets sorted" << endl;
  50. sort (v. begin (), v. end (), [] (auto const & lhs, auto const & rhs) { return lhs.thing.value < rhs.thing.value; });
  51. for (auto const & e : v) {
  52. cout << e. thing. value << " ";
  53. }
  54. cout << endl;
  55. return 0;
  56. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
-------------
construct 0xbfb1d2b4 (1)
copy construct 0xbfb1d294 (1) from 0xbfb1d2b4 (1)
destruct 0xbfb1d2b4 (1)
-------------
construct 0xbfb1d29c (2)
copy construct 0xbfb1d298 (2) from 0xbfb1d29c (2)
destruct 0xbfb1d29c (2)
-------------
copy construct 0xbfb1d2a0 (1) from 0xbfb1d294 (1)
-------------
destruct 0xbfb1d2a0 (1)
copy construct 0xbfb1d2a0 (2) from 0xbfb1d298 (2)
-------------
 a vector with them
construct 0xbfb1d2a4 (3)
copy construct 0xbfb1d2b4 (3) from 0xbfb1d2a4 (3)
copy construct 0xbfb1d2b8 (1) from 0xbfb1d294 (1)
copy construct 0xbfb1d2bc (2) from 0xbfb1d298 (2)
copy construct 0x91eda10 (3) from 0xbfb1d2b4 (3)
copy construct 0x91eda14 (1) from 0xbfb1d2b8 (1)
copy construct 0x91eda18 (2) from 0xbfb1d2bc (2)
destruct 0xbfb1d2bc (2)
destruct 0xbfb1d2b8 (1)
destruct 0xbfb1d2b4 (3)
destruct 0xbfb1d2a4 (3)
gets sorted
copy construct 0xbfb1d25c (1) from 0x91eda14 (1)
destruct 0x91eda14 (1)
copy construct 0x91eda14 (3) from 0x91eda10 (3)
destruct 0x91eda10 (3)
copy construct 0x91eda10 (1) from 0xbfb1d25c (1)
destruct 0xbfb1d25c (1)
copy construct 0xbfb1d20c (2) from 0x91eda18 (2)
destruct 0x91eda18 (2)
copy construct 0x91eda18 (3) from 0x91eda14 (3)
destruct 0x91eda14 (3)
copy construct 0x91eda14 (2) from 0xbfb1d20c (2)
destruct 0xbfb1d20c (2)
1 2 3 
destruct 0x91eda10 (1)
destruct 0x91eda14 (2)
destruct 0x91eda18 (3)
destruct 0xbfb1d2a0 (2)
destruct 0xbfb1d298 (2)
destruct 0xbfb1d294 (1)