Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

`= default` — explicitly defaulted functions in C++

`= default` asks the compiler to supply the standard definition of an eligible function. Ownership, PImpl, and virtual destructors show where this matters.

Version: ISO C++23; implementation support may vary

Syntax

struct T {
    T() = default;
    T(const T&) = default;
    T(T&&) = default;
    T& operator=(const T&) = default;
    T& operator=(T&&) = default;
    ~T();
    bool operator==(const T&) const = default;
};
T::~T() = default;

In C++23, = default; is an explicitly defaulted definition, not an assignment or a switch label (default:). Use it for special member functions (since C++11) and eligible comparison operators (since C++20), such as bool operator==(const T&) const = default;. It cannot define an arbitrary function, and a defaulted function cannot have default arguments. Implementation support may vary.

A function defaulted on its first declaration is user-declared but not user-provided. A later definition such as T::~T() = default; is user-provided, which can affect triviality. If a member or base prevents the required operation, a defaulted function can be defined as deleted. If that happens when it is defaulted after its first declaration, the program is ill-formed.

In const T&, & declares an lvalue reference; in T&&, && declares an rvalue reference. These are typical copy and move parameter types. = delete explicitly prohibits use of a function: if overload resolution selects it, the call is ill-formed. This is not a delete expression, which is used to destroy an object and release its storage. A type holding a std::future can move the handle to the shared state but cannot copy it memberwise.

For PImpl, forward-declare Impl in the header and store a std::unique_ptr<Impl>. The default deleter requires a complete Impl, so define the destructor out of line after Impl is defined. A user-declared destructor suppresses implicit declaration of both move operations; if you need them, declare them and default them there too. Move assignment can delete the destination's previous Impl. The std::unique_ptr is non-copyable, and a constructor's member initializer list initializes the owning member.

In a polymorphic strategy interface, = 0 marks a pure virtual function. By contrast, virtual ~Strategy() = default; defines a public virtual destructor so a derived object is destroyed correctly through std::unique_ptr<Strategy>. The caller injects a strategy through the constructor. A factory registry can map names to factories for strategies already compiled and linked into the program, without loading dynamic libraries.

Examples

#include <iostream>
#include <memory>
#include <stdexcept>
#include <utility>

struct Strategy {
    virtual ~Strategy() = default;
    virtual int apply(int value) const = 0;
};

struct Double final : Strategy {
    int apply(int value) const override { return 2 * value; }
};

class Task {
public:
    explicit Task(std::unique_ptr<Strategy> strategy);
    ~Task();
    Task(Task&&) noexcept;
    Task& operator=(Task&&) noexcept;
    Task(const Task&) = delete;
    Task& operator=(const Task&) = delete;
    int run() const;

private:
    struct Impl;
    std::unique_ptr<Impl> impl_;
};

struct Task::Impl {
    explicit Impl(std::unique_ptr<Strategy> s) : strategy(std::move(s)) {}
    std::unique_ptr<Strategy> strategy;
};

Task::Task(std::unique_ptr<Strategy> s)
    : impl_(std::make_unique<Impl>(std::move(s))) {}
Task::~Task() = default;
Task::Task(Task&&) noexcept = default;
Task& Task::operator=(Task&&) noexcept = default;

int Task::run() const {
    if (!impl_ || !impl_->strategy) {
        throw std::logic_error("no strategy");
    }
    return impl_->strategy->apply(21);
}

int main() {
    Task original(std::make_unique<Double>());
    Task owner(std::move(original));
    std::cout << owner.run() << '\n';
}

Result

42

`main` injects `Double`, then the defaulted move constructor transfers ownership of `Impl` from `original` to `owner`. The virtual call to `Double::apply(21)` produces `42`. The example neither copies the object nor calls `run()` on the moved-from object.

Related lessons

  • C++ · Lesson 45 · Advanced · Version: 3
  • C++ · Lesson 57 · Advanced · Version: 3
  • C++ · Lesson 59 · Advanced · Version: 3

Sources