CKTIS / C++
noexcept — exception specification and operator
noexcept either declares that an exception cannot escape a function or checks at compile time whether an expression is potentially throwing. These two uses combine in the conditional specification noexcept(noexcept(expression)).
Version: C++23; repository compiler flag, production runtime not attested
Syntax
void f() noexcept;
void g() noexcept(constant_expression);
noexcept(expression)
// Conditional specification:
template<class F>
void invoke(F f) noexcept(noexcept(f())) { f(); }After a function declarator, noexcept means the same as noexcept(true): the function has a non-throwing specification. noexcept(false) permits exception propagation but does not require throwing. The condition must be a contextually converted constant expression of type bool.
If an exception attempts to escape a non-throwing function, std::terminate is invoked; an outer catch cannot catch it. Do not assume full stack unwinding. The function may, however, throw an exception and handle it internally.
The operator noexcept(expression) produces a constant bool without executing its operand. The result indicates whether the full expression is potentially throwing under language rules, not whether a particular execution will throw. An ordinary function's empty body alone does not ensure true.
In noexcept(noexcept(f())), the outer noexcept is a specification and the inner one is the operator that determines its condition. The example also shows that checking safe() does not increment calls, whereas actually calling it through invoke does.
The example targets C++23 compilation. The supplied source is an evolving working draft, not a frozen C++23 publication. The displayed code was run unchanged in an isolated environment, and its standard output matched the stated output exactly. Production runtime use is not attested.
Examples
#include <iostream>
int calls = 0;
void safe() noexcept { ++calls; }
void ordinary() {}
template<class F>
void invoke(F f) noexcept(noexcept(f())) {
f();
}
int main() {
static_assert(noexcept(safe()));
static_assert(!noexcept(ordinary()));
static_assert(noexcept(invoke(safe)));
static_assert(!noexcept(invoke(ordinary)));
const bool checked = noexcept(safe());
std::cout << std::boolalpha << checked << ' ' << calls << '\n';
invoke(safe);
std::cout << calls << '\n';
}
Result
true 0 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.