CKTIS / C++
Deleted functions (`= delete`) in C++
`= delete` makes a specific function unusable at compile time. Use it to reject an unwanted overload or to prevent an owning pool from being copied or moved.
Version: ISO C++23; implementation support may vary
Syntax
void f(int);
void f(double) = delete;
struct Pool {
explicit Pool(int value);
Pool(double) = delete;
Pool(const Pool&) = delete;
Pool& operator=(const Pool&) = delete;
Pool(Pool&&) = delete;
Pool& operator=(Pool&&) = delete;
};The feature known as deleted functions (= delete) was introduced in C++11 and is part of ISO C++23. Write = delete; on the function’s first declaration instead of supplying a brace-enclosed body. This works for member functions and for functions at namespace scope. Calling a deleted function, even implicitly, or taking its address is a compile-time error, not a runtime failure.
An explicitly deleted overload still participates in overload resolution. If it wins, the call is ill-formed; the compiler does not fall back to a worse match. For Pool{42.0}, Pool(double) is an exact match, so conversion to int does not rescue the call. This differs from a defaulted move constructor or move assignment operator that is defined as deleted: overload resolution ignores that special member.
Pool owns one slot and stores a pointer to the object in its own buffer. Copying just the pointer value would leave it pointing into the source pool’s buffer. The class therefore explicitly deletes the copy constructor and copy assignment operator as well as the move constructor and move assignment operator.
The standard non-allocating ::new (address) T{...} constructs an object in existing storage with sufficient size and suitable alignment. The explicit destructor call item_->~Item() ends the object’s lifetime without deallocating the buffer. The expressions delete p and delete[] p are for destroying objects and releasing storage obtained through the matching allocating form of new; do not use them on the object in this buffer. = delete itself neither destroys objects nor releases storage.
Examples
#include <cstddef>
#include <iostream>
#include <new>
#include <type_traits>
class Pool {
struct Item { int value; };
alignas(Item) std::byte slot_[sizeof(Item)];
Item* item_;
public:
explicit Pool(int value)
: item_(::new (static_cast<void*>(slot_)) Item{value}) {}
Pool(double) = delete;
Pool(const Pool&) = delete;
Pool& operator=(const Pool&) = delete;
Pool(Pool&&) = delete;
Pool& operator=(Pool&&) = delete;
~Pool() { item_->~Item(); }
int value() const noexcept { return item_->value; }
};
static_assert(!std::is_copy_constructible_v<Pool>);
static_assert(!std::is_copy_assignable_v<Pool>);
static_assert(!std::is_move_constructible_v<Pool>);
static_assert(!std::is_move_assignable_v<Pool>);
int main() {
Pool a{42};
std::cout << a.value() << '\n';
// Pool b = a;
// Pool c{42.0};
}Result
42
The four `static_assert` checks cover copy and move construction and assignment. The program should print `42`. Uncomment either of the last two declarations, one at a time: each should fail to compile because it selects a deleted constructor.
Related lessons
- C++ · Lesson 19 · Advanced · Version: 3
Sources
- Working Draft, Standard for Programming Language C++ (N4950, C++23)
- Deleted definitions [dcl.fct.def.delete]
- Copy/move constructors [class.copy.ctor]
- Copy/move assignment operator [class.copy.assign]
- Non-allocating forms [new.delete.placement]
- Destructors [class.dtor]
- Object lifetime [basic.life]
- Delete [expr.delete]