← C++ roadmap

Topic 26 of 27 · Expert

Implement a minimal move-only owner for one int

Create an owning wrapper that deletes one int, disallows copying and transfers ownership on moves.

ExpertSenior · 5–8 yearsResource ownership

C++20; compile with g++ -std=c++20.

Problem statement

Create an owning wrapper that deletes one int, disallows copying and transfers ownership on moves.

Demonstrate rule-of-five decisions without claiming to replace std::unique_ptr.

Input: A pointer from new int or nullptr.

Output: An owner with get and move operations.

  • Only a single int, never new[].
  • No custom deleters.
  • Use std::unique_ptr in production.

Sample 1

Input: move owner42 into another
Output: old null; new42

Ownership transferred once.

Sample 2

Input: self move assignment
Output: value remains42

Guard prevents deleting its own resource.

Brute-force solution

A copyable raw wrapper shares a pointer and double-deletes; this is invalid, not a slower acceptable solution.

O(1)

#include <utility>
#include <iostream>
#include <cassert>
class IntOwner{int* p;public:explicit IntOwner(int* q=nullptr) noexcept:p(q){}~IntOwner(){delete p;}IntOwner(const IntOwner&)=delete;IntOwner& operator=(const IntOwner&)=delete;IntOwner(IntOwner&& other) noexcept:p(std::exchange(other.p,nullptr)){}IntOwner& operator=(IntOwner&& other) noexcept{if(this!=&other){delete p;p=std::exchange(other.p,nullptr);}return *this;}int* get()const noexcept{return p;}};
int main() {
IntOwner a(new int(42));IntOwner b(std::move(a));assert(a.get()==nullptr && *b.get()==42);b=std::move(b);assert(*b.get()==42);std::cout<<*b.get()<<"\n";
return 0;
}
Optimized solution

Delete copy operations and transfer with std::exchange, clearing the source.

  1. Delete in destructor.
  2. Disable copying.
  3. Implement noexcept moves.
  4. Guard self assignment.
#include <utility>
#include <iostream>
#include <cassert>
class IntOwner{int* p;public:explicit IntOwner(int* q=nullptr) noexcept:p(q){}~IntOwner(){delete p;}IntOwner(const IntOwner&)=delete;IntOwner& operator=(const IntOwner&)=delete;IntOwner(IntOwner&& other) noexcept:p(std::exchange(other.p,nullptr)){}IntOwner& operator=(IntOwner&& other) noexcept{if(this!=&other){delete p;p=std::exchange(other.p,nullptr);}return *this;}int* get()const noexcept{return p;}};
int main() {
IntOwner a(new int(42));IntOwner b(std::move(a));assert(a.get()==nullptr && *b.get()==42);b=std::move(b);assert(*b.get()==42);std::cout<<*b.get()<<"\n";
return 0;
}
Complexity analysis

Time: O(1)

Auxiliary space: O(1)

Test cases
  • Null owner.
  • Self move.
  • Move into an already occupied owner.
Supplied main asserts move transfer and self-move safety.
Common mistakes
  • Using delete for an array.
  • Allowing default shallow copying.
Key takeaways
  • Exactly one owner.
  • Clear moved-from pointer.
  • Prefer standard ownership types.
Follow-up challenges
  • How would a generic deleter be stored?
  • How does rule of zero simplify real application code?
Practise C++ with the AI mentor

Can't find a C++ topic you need for exams or placements? Request it and we'll add it.