Its destructor runs automatically when control leaves the enclosing block. That happens whether you reach the closing brace, return early, or throw.
The order is strictly the reverse of construction. Locals are destroyed newest first, which matters when a later object depends on an earlier one. Inside one object, the destructor body runs, then members in reverse declaration order, then base classes.
{
Logger a;
Connection b(a);
} // ~b runs, then ~a
This order keeps dependent resources safe. A connection that logs during shutdown can rely on the logger still being alive.
Only objects with automatic storage get this, meaning ordinary locals, not static or heap objects. A heap object made with new is untouched by scope exit, and only the pointer to it is destroyed.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓