noexcept
noexcept on a function is a promise that it will not throw. If a noexcept function throws anyway, the program calls std::terminate. The specifier is also a compile-time boolean that std::vector uses to decide whether to move or copy on reallocation.
Detailed Explanation
Move constructors can throw. A std::string move usually does not, but a type that holds two resources and allocates while moving the second one might. std::vector needs to know. When it grows, it moves elements to a new buffer. If a move throws halfway, the old buffer is still intact only if the type's move is known not to throw; otherwise the vector copies instead, because a failed copy leaves the source unchanged.
That is the main reason noexcept exists as a queryable property, not just as documentation. noexcept(expr) is a constant expression, true if expr cannot throw. noexcept(f()) inspects f's exception specification. std::is_nothrow_move_constructible<T> is built on that. Mark your move constructor and move assignment noexcept when they truly cannot throw. std::vector, std::swap, and a lot of generic code will then take the fast path.
The specifier itself: void f() noexcept; or void f() noexcept(true);. noexcept(false) is the default for ordinary functions. Destructors, delete operators, and some defaulted special members are implicitly noexcept unless a member's corresponding operation can throw.
noexcept is not a compiler optimisation fairy. It does not "turn off exceptions" in the body. It is a contract. Break the contract and std::terminate runs immediately: no handler in main is invoked, because unwinding from a noexcept function is not allowed. The process dies.
When to mark it:
- Move constructor and move assignment that only steal pointers and null the source.
swapthat only swaps members which themselves do not throw.- Small observers (
size,empty,data) that cannot allocate or call user code. - A
constexprfunction you have proven cannot throw.
When not to mark it:
- Anything that allocates (
push_backinternals,std::stringappend in the general case). - Anything that calls arbitrary user code (a callback, a virtual function you do not control).
- A function you hope does not throw. Hope is not a specification.
noexcept participates in the type in a limited way. Function pointers have an exception specification as part of the type since C++17: you cannot assign a potentially-throwing function to a noexcept function pointer. Overload resolution can use it. It is still not a reason to write two overloads that differ only in noexcept.
A noexcept specifier can depend on a template parameter: noexcept(std::is_nothrow_copy_constructible_v<T>). That is how a wrapper type forwards the promise of its member.
throw() was the C++03 dynamic specification meaning "throws nothing". It is equivalent to noexcept(true) in C++17 and was removed in C++20. Write noexcept.
Code Examples
#include <iostream>
#include <utility>
#include <vector>
struct Slot {
int* p;
explicit Slot(int n) : p(new int(n)) {}
Slot(const Slot& other) : p(new int(*other.p)) {} // may throw
Slot(Slot&& other) noexcept : p(other.p) { other.p = nullptr; }
Slot& operator=(Slot&& other) noexcept {
if (this != &other) {
delete p;
p = other.p;
other.p = nullptr;
}
return *this;
}
~Slot() { delete p; }
};
int main() {
std::vector<Slot> v;
v.reserve(2);
v.emplace_back(1);
v.emplace_back(2);
v.reserve(16); // moves, because Slot's move is noexcept
std::cout << *v[0].p << '\n'; // prints: 1
}#include <iostream>
#include <type_traits>
void mayThrow();
void noThrow() noexcept;
int main() {
std::cout << std::boolalpha
<< noexcept(mayThrow()) << ' '
<< noexcept(noThrow()) << '\n'; // prints: false true
}Common Gotchas
Throwing through `noexcept` terminates
a `try`/`catch` in `main` will not see it. The specification is checked at the function boundary.
A throwing move makes `vector` copy
if you omit `noexcept` on a move constructor that cannot throw, `std::vector` still copies on grow. That is a silent performance bug, not a compile error.
Implicitly-declared moves become `noexcept(false)` if a member's move can throw
a `std::string` member is fine; a member whose move allocates might poison the whole type.
Destructors are `noexcept` by default
throwing from one calls `terminate`. Catch inside the destructor or do not throw.