// Online C++ compiler to run C++ program online
#include <iostream>
struct Test {
Test() {
std::cout << "Allocating\n";
a = new int(5);
}
Test(Test const & other) {
a = new int(*other.a);
std::cout << "Allocating copy ctr\n";
}
void operator=(Test const & other) {
a = new int(*other.a);
std::cout << "Allocating copy assign\n";
}
~Test() {
std::cout << "Deallocating\n";
delete a;
a = nullptr;
}
int* a;
};
template<typename T>
struct ArrayTest {
void write(T const & test) {
if constexpr (std::is_destructible<T>::value) {
a[0].~Test();
}
a[0] = test;
}
T a[1];
};
int main() {
// Write C++ code here
std::cout << "Creates empty default Test\n";
ArrayTest<Test> a{};
std::cout << "Creates Test b\n";
Test b{};
std::cout << "Creates Test c\n";
Test c{};
std::cout << "Copies b into ArrayTest\n";
a.write(b);
std::cout << "Overwrites b with c in ArrayTest. Should deconstructor b\n";
a.write(c);
std::cout << "Deallocated end of scope\n";
return 0;
}