fork download
  1. #include <iostream>
  2. #include <new>
  3.  
  4. class Test
  5. {
  6. public:
  7. Test() = delete;
  8.  
  9. Test(const Test&) = delete;
  10. Test(Test&&) = delete;
  11. Test& operator=(const Test&) = delete;
  12. Test& operator=(Test&&) = delete;
  13.  
  14. Test(int a, int b) : a_(a), b_(b) { std::cout << "ctor: " << a_ << ' ' << b_ << '\n'; }
  15. virtual ~Test() { std::cout << "dtor: " << a_ << ' ' << b_ << '\n'; }
  16.  
  17. int a_;
  18. int b_;
  19. };
  20.  
  21. class B
  22. {
  23. public:
  24. B()
  25. {
  26. ::new (test_array_storage + 0 * sizeof(Test)) Test(1, 2);
  27. ::new (test_array_storage + 1 * sizeof(Test)) Test(3, 4);
  28. }
  29.  
  30. B(const B&) = delete;
  31. B(B&&) = delete;
  32. B& operator=(const B&) = delete;
  33. B& operator=(B&&) = delete;
  34.  
  35. ~B()
  36. {
  37. reinterpret_cast<Test*>(test_array_storage + 1 * sizeof(Test))->~Test();
  38. reinterpret_cast<Test*>(test_array_storage + 0 * sizeof(Test))->~Test();
  39. }
  40.  
  41. private:
  42. alignas(Test) unsigned char test_array_storage[sizeof(Test) * 2];
  43. };
  44.  
  45. int main()
  46. {
  47. B b;
  48. Test test[2] = {{1, 2}, {3, 4}};
  49. }
  50.  
Success #stdin #stdout 0s 4356KB
stdin
Standard input is empty
stdout
ctor: 1 2
ctor: 3 4
ctor: 1 2
ctor: 3 4
dtor: 3 4
dtor: 1 2
dtor: 3 4
dtor: 1 2