`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.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

noexcept

Has 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.

Syntax

return-type func(params) noexcept;           // specifier: always non-throwing
return-type func(params) noexcept(expr);     // conditional noexcept

noexcept(expression)                         // operator: true if non-throwing

Example

#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
}

Notes

Example in practice

int 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;
}