`noexcept` — C++ Keyword
`noexcept` — C++ Keyword
The noexcept keyword in C++: declares that a function will not throw, and tests whether an expression can throw.
`noexcept` — C++ Keyword
The noexcept keyword in C++: declares that a function will not throw, and tests whether an expression can throw.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
noexceptHas two forms: (1) a specifier that declares a function will not propagate exceptions, and (2) an operator that evaluates at compile time whether an expression is declared non-throwing.
return-type func(params) noexcept; // specifier: always non-throwing
return-type func(params) noexcept(expr); // conditional noexcept
noexcept(expression) // operator: true if non-throwing
#include <print>
#include <vector>
#include <type_traits>
class Buffer {
std::vector<int> data_;
public:
Buffer() noexcept = default;
// noexcept(noexcept(...)) – propagate noexcept from move of member
Buffer(Buffer&& o) noexcept(noexcept(std::vector<int>(std::move(o.data_))))
: data_(std::move(o.data_)) {}
void push(int v) {
data_.push_back(v); // may throw std::bad_alloc; not noexcept
}
};
int main() {
std::println("{}", noexcept(Buffer{})); // true
}
noexcept when possible; std::vector uses the move operation conditionally based on this.noexcept function that does throw calls std::terminate.noexceptint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}