Skip to main content

Lesson 5 of 5

Practice & Quiz - Rule of 5 & Advanced Class Design

Test your command of the six special member functions, Rule of 0/3/5, copy-and-swap, constexpr, and if constexpr with a practical exercise and 5 quiz questions.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Practical Exercise - Handle<T> RAII Wrapper (Complete Rule of 5)

Practical Exercise - Handle<T> RAII Wrapper (Complete Rule of 5)
#include <iostream>
#include <utility>
#include <cassert>

// Generic RAII handle wrapper - models unique ownership of a resource
// Simulates something like a file descriptor or OS handle
template<typename T>
class Handle {
    T    value_;
    bool valid_;

    static void release_resource(T val) {
        std::cout << "  [release " << val << "]\n";
    }

public:
    // 1. Default ctor - invalid handle
    Handle() noexcept : value_(T{}), valid_(false) {}

    // 2. Acquiring ctor - takes ownership
    explicit Handle(T val) noexcept : value_(val), valid_(true) {}

    // 3. Copy is DELETED - handles are unique
    Handle(const Handle&) = delete;
    Handle& operator=(const Handle&) = delete;

    // 4. Move ctor - transfer ownership
    Handle(Handle&& other) noexcept
        : value_(other.value_), valid_(other.valid_) {
        other.valid_ = false;   // source is now invalid
    }

    // 5. Move assignment - release own, then transfer
    Handle& operator=(Handle&& other) noexcept {
        if (this != &other) {
            if (valid_) release_resource(value_);
            value_       = other.value_;
            valid_       = other.valid_;
            other.valid_ = false;
        }
        return *this;
    }

    // 6. Destructor - release if valid
    ~Handle() noexcept {
        if (valid_) release_resource(value_);
    }

    bool     valid() const noexcept { return valid_; }
    T        get()   const noexcept { return value_; }
    explicit operator bool() const noexcept { return valid_; }

    // Release and return value (like unique_ptr::release)
    T release() noexcept {
        valid_ = false;
        return value_;
    }
};

int main() {
    Handle<int> h1(42);
    std::cout << "h1 valid=" << h1.valid() << " val=" << h1.get() << "\n";

    Handle<int> h2 = std::move(h1);   // move ctor
    std::cout << "h1 valid=" << h1.valid() << " (moved-from)\n";
    std::cout << "h2 valid=" << h2.valid() << " val=" << h2.get() << "\n";

    Handle<int> h3;
    h3 = std::move(h2);               // move assign - h2 becomes invalid
    std::cout << "h3 valid=" << h3.valid() << " val=" << h3.get() << "\n";

    // constexpr check
    static_assert(!std::is_copy_constructible_v<Handle<int>>);
    static_assert(std::is_move_constructible_v<Handle<int>>);

    std::cout << "Leaving scope - h3 releases:\n";
    return 0;   // h1,h2 invalid - no release; h3 valid - releases
}

Module Quiz

Module Quiz
Question 1 of 5

If you declare a DESTRUCTOR in a class but nothing else, what happens to the auto-generated move constructor and move assignment?

Tip

Tip

Practice Practice Quiz Rule of 5 Advanced Class Design in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Special Member Function Generation, Full String Class Implementation & Compile-Time constexpr

Practice Task

Note

Practice Task - (1) Write a working example of Practice Quiz Rule of 5 Advanced Class Design from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Practice Quiz Rule of 5 Advanced Class Design is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.

Topics in This Module

0/5 done