5.2.30 · D1C++ Programming

Foundations — noexcept specifier

1,573 words7 min readBack to topic

Before you can trust the parent note on the `noexcept` specifier, you need to see what an exception actually is, what "escaping a function" looks like, and what the tiny words like bool, template, and std::move mean. This page builds every one of those from zero, in the order they stack on each other.


0 · What is a function call, drawn as a stack?

Everything about noexcept is about what happens when things go wrong between function calls. So we must first picture calls.

Figure — noexcept specifier

Look at the tower of frames. Normal return = pop the top plate and continue below. The whole story of exceptions is: what if we need to remove several plates at once because of an error?


1 · What is an exception, and what does "escape" mean?

Figure — noexcept specifier

In the figure, follow the amber path. b throws. There is no net in b, so b's frame is destroyed and the signal drops to a. No net in a either — a's frame is destroyed too. Finally main has a catch, so the signal is caught there.

This word escape is the heart of noexcept. The promise is precisely: "an exception will never escape me." Not "I never throw internally" — only that nothing leaves.


2 · Destructors — the cleanup that runs during unwinding

Here is the danger the parent hints at. During unwinding we are already carrying one exception. If a destructor also throws mid-unwinding, we now have two exceptions travelling at once — the runtime cannot choose which to follow, so the language says: this is illegal, call std::terminate. That is exactly why Destructors are implicitly noexcept since C++11.

See Exception handling in C++ for the full mechanics of throw/catch.


3 · std::terminate — the kill switch


4 · bool and compile-time — what noexcept(expr) produces

The parent uses noexcept in a second role: as an operator returning a bool. Two words to unlock.

Because it is a compile-time bool, you can store it in a constexpr variable and branch on it in generic code.


5 · template<class T> — code with a type-shaped hole

We need templates because whether a function should be noexcept often depends on T — a swap of cheap ints never throws, but a swap of a throw-prone type might. Hence conditional noexcept.


6 · std::move and move semantics — why vector cares

Figure — noexcept specifier

Prerequisite map

Call stack and frames

Exceptions throw and catch

Stack unwinding and escape

Destructors run during unwinding

std terminate kills on broken promise

bool and compile time

noexcept operator returns bool

templates fill a type hole

conditional noexcept

copy vs move semantics

vector move or copy choice

noexcept specifier

Each arrow means "you must understand the left box before the right box makes sense." All roads feed the parent topic at the bottom.


Equipment checklist

Test yourself — reveal only after you've answered aloud.

A stack frame is
the box holding one function call's local variables; calls stack on top of each other.
"Stack unwinding" means
destroying frames one by one as an exception drops down looking for a matching catch.
An exception "escapes" a function when
it leaves the function without being caught inside it (only this triggers the noexcept penalty).
std::terminate does
ends the program immediately with no guaranteed further cleanup.
A bool is
a value that is either true or false.
"Compile-time" vs "run-time"
compile-time is while the program is being built (before it runs); run-time is while it is running.
noexcept(expr) as an operator evaluates expr how
it is unevaluated — the compiler inspects it for throwing but never runs it, returning a compile-time bool.
template<class T> is
a recipe with a type-shaped placeholder T, stamped into a real function per concrete type.
Difference between copy and move
copy duplicates (original intact); move steals internals (original gutted) — cheaper.
std::vector prefers moving on reallocation only when
the move constructor is noexcept, otherwise it copies to keep the strong exception guarantee.