fork download
  1. #include <iostream>
  2.  
  3. template<typename T>
  4. struct Wrapper
  5. {
  6. T &t;
  7. Wrapper(T &t)
  8. : t(t)
  9. {
  10. }
  11. void rebind(T &t)
  12. {
  13. this->~Wrapper();
  14. new (this) Wrapper(t);
  15. }
  16. };
  17.  
  18. int main()
  19. {
  20. int x = 7, y = 4;
  21. Wrapper<int> w = x;
  22. std::cout << x << " " << y << std::endl;
  23. w.t = 2;
  24. std::cout << x << " " << y << std::endl;
  25. w.rebind(y);
  26. std::cout << x << " " << y << std::endl;
  27. w.t = 14;
  28. std::cout << x << " " << y << std::endl;
  29. }
  30.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
7 4
2 4
2 4
2 14