#include <memory>
#include <iostream>
/* copyable unique_ptr
* https://t...content-available-to-author-only...s.com/2015/06/27/cs-rule-of-zero/ */
//self-contained version for C++11
template <class T> class copying_unique_ptr : public std::unique_ptr<T> {
public:
copying_unique_ptr(std::unique_ptr<T> &&t)
: std::unique_ptr<T>(std::forward(t)){};
copying_unique_ptr(T *in) : std::unique_ptr<T>(in) {}
copying_unique_ptr(copying_unique_ptr<T> &&other) = default;
copying_unique_ptr &operator=(copying_unique_ptr &&other) = default;
copying_unique_ptr(const copying_unique_ptr<T> &other)
: copying_unique_ptr(new T(*other)) {}
copying_unique_ptr &operator=(const copying_unique_ptr &other) {
std::swap(*this, copying_unique_ptr(other));
return *this;
}
};
struct DestructorInt {
int i;
~DestructorInt() {
std::cout << "Destructor here; i = " << i << std::endl;
}
};
int main() {
using namespace std;
std::unique_ptr<int> up(new int(3));
cout << *up << endl;
copying_unique_ptr<int> cp(new int(3));
cout << *cp << endl;
// copy_ptr<int> cp; //(make_unique<int>(3));
// IntCopyPtr icp(make_unique<int>(3));
auto cp2 = cp;
cout << *cp2 << " <- copy / orig -> " << *cp << endl;
*cp2 = 5; *cp = 2;
cout << *cp2 << " <- copy / orig -> " << *cp << endl;
copying_unique_ptr<DestructorInt> dest1(new DestructorInt);
auto dest2 = dest1;
dest1->i = 85;
dest2->i = 37;
}