fork(1) download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. struct Atom
  5. {
  6. std::aligned_storage_t<sizeof(T), alignof(T)> storage;
  7. template <typename... Args>
  8. void initialize(Args&&... args)
  9. {
  10. new (&storage) T(std::forward<Args>(args)...);
  11. }
  12.  
  13. T& get()
  14. {
  15. return *reinterpret_cast<T*>(&storage);
  16. }
  17.  
  18. void destroy()
  19. {
  20. get().~T();
  21. }
  22. };
  23.  
  24. int main()
  25. {
  26. Atom<int> a;
  27. a.initialize(1);
  28.  
  29. Atom<int> b;
  30. b.initialize(2);
  31.  
  32. std::swap(a, b);
  33.  
  34. std::cout << a.get() << " " << b.get() << std::endl;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
2 1