CKTIS / C++
`= delete`: deleted functions and exclusive ownership
`= delete` forbids use of a function at compile time. A coroutine frame owner can use it to forbid copying while still transferring ownership by moving and releasing the frame in its destructor.
Version: ISO C++23; implementation support may vary
Syntax
void f(int);
void f(double) = delete;
struct Task {
Task() noexcept = default;
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;
Task(Task&&) noexcept;
Task& operator=(Task&&) noexcept;
~Task();
};= delete; explicitly defines a function as deleted and must appear on its first declaration. It does not remove the function from overload resolution. If overload resolution selects a deleted function, the program is ill-formed: for f(3.5), the deleted f(double) beats f(int), and there is no fallback to f(int). Taking the address of a deleted function is also ill-formed.
Delete the copy constructor and copy assignment operator to prevent two owners from claiming the same frame. In const Task&, & declares an lvalue reference; in Task&&, && declares an rvalue reference. std::move does not transfer the resource by itself: when moving between distinct objects, the owner's move operations transfer the handle and empty the source. Move assignment between distinct objects first releases any frame already owned by the destination. Self-move assignment leaves the object unchanged.
~Task() is a destructor, not the bitwise complement operator ~. For a nonempty handle, the owner calls destroy() only if the handle refers to a live, suspended coroutine. = delete does not destroy an object or free storage; delete p and delete[] p are separate runtime expressions.
= default requests the definition prescribed by the language rules, which can still result in a deleted function; it is not a switch label such as default:. Here it provides an empty initial state thanks to h_{}. Defaulted moves of the coroutine handle itself would not empty the source. The = delete; syntax is available since C++11; the example targets C++23, and implementation support may vary.
Examples
#include <coroutine>
#include <cstdio>
#include <exception>
#include <utility>
class Task {
public:
struct promise_type;
private:
using Handle = std::coroutine_handle<promise_type>;
Handle h_{};
explicit Task(Handle h) noexcept : h_(h) {}
public:
struct promise_type {
Task get_return_object() noexcept {
return Task{Handle::from_promise(*this)};
}
std::suspend_always initial_suspend() noexcept { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_void() noexcept {}
void unhandled_exception() noexcept { std::terminate(); }
~promise_type() { std::puts("promise destroyed"); }
};
Task() noexcept = default;
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;
Task(Task&& other) noexcept : h_(std::exchange(other.h_, Handle{})) {}
Task& operator=(Task&& other) noexcept {
if (this != &other) {
if (h_) h_.destroy();
h_ = std::exchange(other.h_, Handle{});
}
return *this;
}
~Task() {
if (h_) h_.destroy();
}
};
Task make_task() { co_return; }
int main() {
Task a = make_task();
// Task copy = a;
Task b{std::move(a)};
Task c;
// c = b;
c = std::move(b);
std::puts("leaving scope");
}Result
leaving scope promise destroyed
The coroutine remains at its initial suspension point. After two moves, only `c` owns the frame; its destructor calls `destroy()`, so the promise destructor prints `promise destroyed` once. Uncomment each copy line separately to test rejection.
Related lessons
- C++ · Lesson 41 · Advanced · Version: 3
- C++ · Lesson 42 · Advanced · Version: 3
- C++ · Lesson 43 · Advanced · Version: 3
- C++ · Lesson 45 · Advanced · Version: 3
Sources
- Working Draft, Standard for Programming Language C++ (N4950, C++23)
- Deleted definitions [dcl.fct.def.delete]
- Explicitly-defaulted functions [dcl.fct.def.default]
- Copy/move constructors [class.copy.ctor]
- Copy/move assignment operator [class.copy.assign]
- References [dcl.ref]
- Destructors [class.dtor]
- Delete [expr.delete]
- Coroutines [dcl.fct.def.coroutine]
- Coroutine handle resumption and destruction [coroutine.handle.resumption]