#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct test {
 int const value;
 test (int v)  : value (v) {
  cout << "construct " << this << " (" << value << ")"<< endl;
 }
 test (test const & other) : value (other. value){
  cout << "copy construct " << this << " (" << value << ")"<< " from " << (& other) << " (" << value << ")"<< endl;
 }
 ~test () {
  cout << "destruct " << this << " (" << value << ")" << endl;
 }
};
 


template<typename T>
union ac {
 T thing;
 ac & operator=(ac<T> const & other) {
  thing. ~T();
  new (& thing) T(other. thing);
 }
 ac(T && t) : thing (std:: forward<T>(t))
 {}
 ac(ac<T> const & other) : thing (other. thing) {}
 ~ac() {
  thing. ~T();
 }
};


int main() {
 cout << "-------------" << endl;
 ac<test> one {test {1}};
 cout << "-------------" << endl;
 ac<test> two {test {2}};
 cout << "-------------" << endl;
 ac<test> also_one {one};
 cout << "-------------" << endl;
 also_one = two;
 cout << "-------------" << endl;
 cout << " a vector with them" << endl;
 vector<ac<test>> v {test {3}, one, two};
 cout << "gets sorted" << endl;
 sort (v. begin (), v. end (), [] (auto const & lhs, auto const & rhs) { return lhs.thing.value < rhs.thing.value; });
 for (auto const & e : v) {
  cout << e. thing. value << " ";
 }
 cout << endl;
 return 0;
}