CKTIS / C++
Deleted function (`= delete`)
Reject a specific overload or prevent copying at compile time while keeping a class movable and handling destruction of its hidden implementation correctly.
Version: ISO C++23; implementation support may vary
Syntax
void f(int) = delete;
// Inside class T:
T(const T&) = delete;
T& operator=(const T&) = delete;In C++23, a deleted function (= delete) can be explicitly defined by writing = delete; on its first declaration. You cannot call it or take its address. This works for both member functions and namespace-scope functions. It is not a delete-expression such as delete p;, which is used to destroy an object and release its storage, not to disable a function.
An overload explicitly deleted with = delete still participates in overload resolution. With void f(double); and void f(int) = delete;, f(1) selects the exact match f(int) and is ill-formed: the compiler does not fall back to f(double). The call f(1.0) selects f(double). By contrast, a defaulted move constructor or move assignment operator that is defined as deleted is ignored by overload resolution.
Widget deletes its copy constructor and copy assignment operator. It uses = default for the move constructor, move assignment operator, and destructor, asking the compiler to generate them according to the language rules. A defaulted function can still be defined as deleted if a required operation on a member or base class is unavailable.
In the PImpl idiom, std::unique_ptr<Impl> can be declared while Impl is incomplete, but the default deleter needs a complete type. This matters for destruction and for move assignment, which can destroy the object previously owned by the destination. The example defines the destructor and both move operations out of line, after defining Impl. When splitting the code, keep the declarations in the header and put these definitions in the .cpp file.
Examples
#include <iostream>
#include <memory>
#include <type_traits>
#include <utility>
class Widget {
public:
Widget();
~Widget();
Widget(const Widget&) = delete;
Widget& operator=(const Widget&) = delete;
Widget(Widget&&) noexcept;
Widget& operator=(Widget&&) noexcept;
bool has_impl() const noexcept { return static_cast<bool>(impl_); }
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};
struct Widget::Impl {};
Widget::Widget() : impl_(std::make_unique<Impl>()) {}
Widget::~Widget() = default;
Widget::Widget(Widget&&) noexcept = default;
Widget& Widget::operator=(Widget&&) noexcept = default;
static_assert(!std::is_copy_constructible_v<Widget>);
static_assert(!std::is_copy_assignable_v<Widget>);
static_assert(std::is_move_constructible_v<Widget>);
static_assert(std::is_move_assignable_v<Widget>);
int main() {
Widget a;
// Widget copy(a);
Widget b(std::move(a));
Widget c;
// c = b;
c = std::move(b);
std::cout << std::boolalpha << a.has_impl() << ' '
<< b.has_impl() << ' ' << c.has_impl() << '\n';
}
Result
false false true
The `static_assert` checks show that copying is disabled and moving is supported. The output `false false true` means that only `c` owns an `Impl` after both moves. Uncomment either copy attempt on its own to see a compile-time error.
Related lessons
- C++ · Lesson 11 · Advanced · Version: 3
Sources
- Deleted definitions [dcl.fct.def.delete]
- Explicitly-defaulted functions [dcl.fct.def.default]
- Delete [expr.delete]
- Default deleter for single objects [unique.ptr.dltr.dflt]
- unique_ptr constructors [unique.ptr.single.ctor]
- unique_ptr destructor [unique.ptr.single.dtor]
- unique_ptr assignment [unique.ptr.single.asgn]
- C++23 working draft N4950