Unique Pointer
Sole ownership
OpenImplement the constructors, the destructor, release and reset. The accessors are written for you. Copying and moving are deleted in the scaffold and are not tested here.
The tests count constructions and destructions and abort on a double destroy, and they audit every allocation, so each object a UniquePtr owns has to be deleted exactly once and an object it gave away through release must not be deleted by it at all.
Two element types watch their owner from inside their own destructor: one records what get() returns while it is being deleted, and one calls reset() on the UniquePtr that is deleting it.
Implement UniquePtr<T>, a smart pointer with exclusive ownership of one heap object, mirroring the core of std::unique_ptr<T>.
UniquePtr<T> owns at most one heap object, created with new, and deletes it when the UniquePtr is destroyed or handed something else to own. Ownership is exclusive: a UniquePtr cannot be copied. It can be moved, and a move hands the object to the destination and leaves the source empty.
template <class T>
class UniquePtr {
public:
UniquePtr() noexcept; // owns nothing
explicit UniquePtr(T* ptr) noexcept; // takes ownership of ptr, which may be null
~UniquePtr(); // deletes the owned object, if any
UniquePtr(const UniquePtr&) = delete;
UniquePtr& operator=(const UniquePtr&) = delete;
UniquePtr(UniquePtr&& other) noexcept; // Part 2
template <class U>
UniquePtr(UniquePtr<U>&& other) noexcept; // Part 2, when U* converts to T*
UniquePtr& operator=(UniquePtr&& other) noexcept; // Part 2
T* get() const noexcept;
T& operator*() const;
T* operator->() const noexcept;
explicit operator bool() const noexcept;
T* release() noexcept; // gives up ownership, deletes nothing
void reset(T* ptr = nullptr) noexcept; // owns ptr, deletes the old object
void swap(UniquePtr& other) noexcept; // Part 2
};- An empty
UniquePtrholds a null pointer. Destroying or resetting it deletes nothing, and it never allocates. get(),operator*andoperator->do not transfer ownership. They areconstbecause the constness of the pointer does not reach the object: aconst UniquePtr<T>still gives a mutableT.release()returns the owned pointer and leaves theUniquePtrempty. The caller owns the object from then on.reset(ptr)makesptrthe owned pointer and then deletes the previous object, if there was one. The order is part of the contract: while the old object is being deleted,get()already returnsptr.- After
a = std::move(b),aowns whatbowned, the objectaowned before is deleted, andbis empty.a = std::move(a)leavesaowning its object. - The converting move constructor accepts a
UniquePtr<U>whenever aU*converts to aT*, which is how aUniquePtr<Derived>becomes aUniquePtr<Base>. The tests giveBasea virtual destructor, so deleting through aBase*is correct.
Every object a UniquePtr owns is deleted exactly once, with delete, and an object it gave away through release() is never deleted by it. The tests count constructions and destructions of their element types, abort on a double destroy, and audit every allocation a scenario makes. Freed memory is overwritten before it is reused, so reading an object after deleting it fails a check rather than reading stale values.
The harness defines unique_ptr, shared_ptr, weak_ptr, auto_ptr, make_unique and make_shared away, so none of them may appear in a submission, member and variable names included. Do not write main; the harness owns it. The standard headers are already included.
struct Widget { int id; explicit Widget(int i) : id(i) {} };
UniquePtr<Widget> a(new Widget(1));
a->id; // 1
a.reset(new Widget(2)); // Widget 1 deleted, a owns Widget 2
Widget* raw = a.release(); // a is empty; the caller owns Widget 2
delete raw;
UniquePtr<Widget> b(new Widget(3));
UniquePtr<Widget> c = std::move(b); // Part 2: c owns Widget 3, b is emptyNone. The judge compiles your class into a hidden harness that drives it directly and reports one row per scenario.
None. Your code prints nothing; the harness owns the verdict.
operator*andoperator->on an emptyUniquePtrare undefined behaviour. The tests never do it.The tests only ever hand a
UniquePtra pointer that came fromnew, or a null pointer.Single-threaded. No custom deleters and no array form.
sole-ownership
PASS