CKTIS / C++
Defaulted special member functions (`= default`) in C++
In C++23, `= default` asks the compiler for the standard definition of a special member function. Definition placement matters in Pimpl; C++23 implementation support may vary.
Version: ISO C++23; implementation support may vary
Syntax
struct S {
S() = default;
S(const S&) = default;
S(S&&) = default;
S& operator=(const S&) = default;
S& operator=(S&&) = default;
~S();
};
S::~S() = default;A defaulted special member (= default) asks the compiler to define a default constructor, copy or move constructor, copy or move assignment operator, or destructor according to the language rules. It is not an empty {} body: the generated function applies the rules for that operation to base-class subobjects and non-static data members.
A function defaulted on its first declaration is user-declared but not user-provided. It can be trivial if the other conditions hold, or be defined as deleted if a required subobject operation is unavailable. A function defaulted after its first declaration is user-provided and is not trivial. If its generated definition would be deleted, the program is ill-formed.
= delete is different: a deleted overload still participates in overload resolution, but selecting it is an error. It is not a delete p expression and does not free memory. A user-declared destructor, even a defaulted one, suppresses implicit declaration of the move operations. Widget therefore declares both moves explicitly and deletes both copy operations.
With Pimpl, the header can forward-declare Impl and hold a std::unique_ptr<Impl>. The default deleter needs Impl to be complete: destruction can delete the owned object, and move assignment can delete the target's previous object. Declare the functions in the header and default the destructor and both moves in the implementation file after defining Impl. Placing the move constructor there also keeps any exception cleanup where Impl is complete: moving the pointer itself does not delete the object, but construction can require cleanup of subobjects if an exception occurs.
Examples
#include <iostream>
#include <memory>
#include <utility>
class Widget {
public:
explicit Widget(int value);
~Widget();
Widget(const Widget&) = delete;
Widget& operator=(const Widget&) = delete;
Widget(Widget&&) noexcept;
Widget& operator=(Widget&&) noexcept;
int value() const noexcept;
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};
struct Widget::Impl {
int value;
};
Widget::Widget(int value) : impl_(std::make_unique<Impl>(Impl{value})) {}
Widget::~Widget() = default;
Widget::Widget(Widget&&) noexcept = default;
Widget& Widget::operator=(Widget&&) noexcept = default;
int Widget::value() const noexcept {
return impl_ ? impl_->value : -1;
}
int main() {
Widget source(42);
Widget intermediate(std::move(source));
Widget target(7);
target = std::move(intermediate);
std::cout << target.value() << '\n'
<< source.value() << '\n'
<< intermediate.value() << '\n';
}Result
42 -1 -1
One file models the header/implementation split. Move construction followed by move assignment transfers the object holding `42` to `target`, replacing the object holding `7`. Both source pointers are empty, so `value()` returns `-1` for each.
Related lessons
- C++ · Lesson 11 · Advanced · Version: 3