5.2.30 · D3C++ Programming

Worked examples — noexcept specifier

2,367 words11 min readBack to topic

This page is a child of noexcept specifier. If a term below feels unearned, the parent builds it first.


The scenario matrix

Every question noexcept can ask falls into one of these cells. Each worked example is tagged with the cell(s) it covers.

# Case class The concrete question Expected outcome
A Operator on a promised function noexcept(safe()) where safe is noexcept true
B Operator on a default function noexcept(risky()) where risky may throw false
C Escape (contract broken) throw leaves a noexcept function std::terminate()
D Caught inside (contract kept) throw is caught within the noexcept function fine, no terminate
E Conditional true branch a template whose move can't throw noexcept(...)true
F Conditional false branch a template whose move might throw noexcept(...)false
G std::vector reallocation grow a vector of a move-noexcept type vs not move vs copy
H Degenerate: destructor implicit noexcept on ~S throwing → terminate
I Degenerate: empty / non-throwing ops noexcept(1 + 2), noexcept(x) on plain reads true
J Exam twist: nested operator noexcept(noexcept(x)) — which is specifier? outer = specifier

Rows A/B are the two truth values of the operator; C/D are the two sides of the escape boundary; E/F are the two branches of a conditional; G is the payoff; H/I/J are the degenerate and trick corners. Nothing about noexcept lives outside this table.


Example 1 — Cells A & B: the two truth values of the operator

Cell A gave true, Cell B gave false. Both truth values now exist.


Example 2 — Cell C: an exception that escapes → terminate



Example 4 — Cells E & F: conditional noexcept, both branches


Example 5 — Cell G: why std::vector reallocation branches (with figure)


Example 6 — Cell H: destructors are implicitly noexcept


Example 7 — Cell I: degenerate expressions the operator sees as non-throwing


Example 8 — Cell J (exam twist): decode noexcept(noexcept(x))


Recall Did every cell get hit?

Which examples cover the two operator truth values? ::: Example 1 (Cells A true, B false). Which two examples are the escape-vs-caught pair? ::: Example 2 (Cell C, escape → terminate) and Example 3 (Cell D, caught → fine). Which example produces both conditional-noexcept branches? ::: Example 4 (Cells E and F). Which example is the vector move-vs-copy payoff? ::: Example 5 (Cell G). Which examples are the degenerate corners? ::: Example 6 (Cell H, destructor), Example 7 (Cell I, pure expressions). Which example is the specifier-vs-operator exam twist? ::: Example 8 (Cell J).


Recall flashcards

What decides whether a throw in a noexcept function calls terminate?
Whether it escapes the function; caught internally is fine, crossing the boundary is fatal.
For vector<Fast> (move noexcept) vs vector<Slow> (move may throw), what does reallocation do?
Moves the Fast elements (fast path), copies the Slow elements (safe path) to preserve the strong guarantee.
In noexcept(noexcept(x)), which is the specifier?
The outer one (it sits in the specifier slot after the signature); the inner is the operator.
Is noexcept(1 + 2) true or false?
true — built-in arithmetic has no throwing operation.
Are destructors that omit noexcept may-throw?
No — since C++11 they are implicitly noexcept; throwing from one calls terminate.