fork(1) download
  1. #include <memory>
  2. #include <vector>
  3. #include <boost/variant.hpp>
  4.  
  5. using namespace std;
  6.  
  7. class holder;
  8. struct v_with_holder {
  9. // a bunch of fields
  10. std::unique_ptr<holder> ph;
  11. holder& h;
  12.  
  13. friend void swap(v_with_holder& first, v_with_holder& second)
  14. {
  15. using std::swap;
  16. // swap a bunch of fields
  17. swap(first.ph, second.ph);
  18. }
  19.  
  20. v_with_holder();
  21. ~v_with_holder();
  22. v_with_holder(const v_with_holder& other);
  23. v_with_holder(v_with_holder&& other);
  24. v_with_holder& operator=(v_with_holder other);
  25. };
  26.  
  27. typedef boost::variant</* such types, */v_with_holder/*, many others */> struct_v;
  28.  
  29. class holder { public: std::vector<struct_v> ss; };
  30.  
  31. v_with_holder::v_with_holder() : ph(new holder), h(*ph) { }
  32. v_with_holder::~v_with_holder() { }
  33. v_with_holder::v_with_holder(const v_with_holder& other) : ph(new holder), h(*ph)
  34. {
  35. // copy a bunch of fields
  36. h = other.h;
  37. }
  38. v_with_holder::v_with_holder(v_with_holder&& other) : v_with_holder()
  39. {
  40. swap(*this, other);
  41. }
  42. v_with_holder& v_with_holder::operator=(v_with_holder other)
  43. {
  44. swap(*this, other);
  45. return *this;
  46. }
  47.  
  48. int main() {
  49. holder h1, h2;
  50. v_with_holder x;
  51. x.h = h2;
  52. h1.ss.push_back(x);
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 3300KB
stdin
Standard input is empty
stdout
Standard output is empty