fork(1) download
  1. #include <utility>
  2. #include <new>
  3.  
  4. using std::move;
  5.  
  6. template <typename T>
  7. class container
  8. {
  9. public:
  10. void add(T const & val)
  11. {
  12. obj_.~T(); // ignore this shit
  13. new (&obj_) T{val}; // force to use cctor
  14. }
  15.  
  16. void add(T && val)
  17. {
  18. obj_.~T();
  19. new (&obj_) T{move(val)}; // force to use mctor
  20. }
  21.  
  22. private:
  23. T obj_;
  24. };
  25.  
  26. struct moveable
  27. {
  28. moveable() = default;
  29. moveable(moveable const &) = delete;
  30. moveable(moveable &&) = default;
  31. };
  32.  
  33. int main()
  34. {
  35. container<moveable> c;
  36. c.add(moveable{});
  37. }
  38.  
Success #stdin #stdout 0s 3336KB
stdin
Standard input is empty
stdout
Standard output is empty