Namespaces
Variants

noexcept operator (since C++11)

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

noexcept 演算子は、式が例外を投げないと宣言されている場合に true を返すコンパイル時チェックを実行します。

これは関数テンプレート内の noexcept 指定子 内で使用でき、関数が一部の型に対しては例外をスローするが、他の型に対してはスローしないことを宣言するために使用されます。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - HTMLタグ、属性、 内のC++関連用語(Syntax、Notes、Keywords、Example、Defect reports、See also)は翻訳せずに保持しました - 数値、クラス名、ID、リンク先などはすべて元のまま維持しています - フォーマットと構造は完全に保持されています

構文

noexcept( )

bool prvalue を返す。結果は、 expression potential exceptions の集合が空である場合 (C++17まで) expression non-throwing と指定されている場合 (C++17以降) true であり、それ以外の場合は false である。

expression 未評価オペランド です。

expression がprvalueの場合、 一時オブジェクト化 が適用されます。

(C++17以降)

注記

noexcept ( expr ) true であっても、 expr の評価は未定義動作に遭遇した結果として、依然として例外を送出する可能性があります。

expression がクラス型または(多次元の可能性がある)配列である場合、一時実体化にはデストラクタが削除されておらずアクセス可能であることが必要です。

(C++17以降)

キーワード

noexcept

#include <iostream>
#include <utility>
#include <vector>
void may_throw();
void no_throw() noexcept;
auto lmay_throw = []{};
auto lno_throw = []() noexcept {};
class T
{
public:
    ~T(){} // デストラクタがムーブコンストラクタを妨げる
           // コピーコンストラクタはnoexcept
};
class U
{
public:
    ~U(){} // デストラクタがムーブコンストラクタを妨げる
           // コピーコンストラクタはnoexcept(false)
    std::vector<int> v;
};
class V
{
public:
    std::vector<int> v;
};
int main()
{
    T t;
    U u;
    V v;
    std::cout << std::boolalpha <<
        "may_throw() is noexcept(" << noexcept(may_throw()) << ")\n"
        "no_throw() is noexcept(" << noexcept(no_throw()) << ")\n"
        "lmay_throw() is noexcept(" << noexcept(lmay_throw()) << ")\n"
        "lno_throw() is noexcept(" << noexcept(lno_throw()) << ")\n"
        "~T() is noexcept(" << noexcept(std::declval<T>().~T()) << ")\n"
        // 注意: 以下のテストでは、noexcept内の式が一時オブジェクトを構築・破棄するため、
        // ~T()がnoexceptであることも必要
        "T(rvalue T) is noexcept(" << noexcept(T(std::declval<T>())) << ")\n"
        "T(lvalue T) is noexcept(" << noexcept(T(t)) << ")\n"
        "U(rvalue U) is noexcept(" << noexcept(U(std::declval<U>())) << ")\n"
        "U(lvalue U) is noexcept(" << noexcept(U(u)) << ")\n"
        "V(rvalue V) is noexcept(" << noexcept(V(std::declval<V>())) << ")\n"
        "V(lvalue V) is noexcept(" << noexcept(V(v)) << ")\n";
}

出力:

may_throw() is noexcept(false)
no_throw() is noexcept(true)
lmay_throw() is noexcept(false)
lno_throw() is noexcept(true)
~T() is noexcept(true)
T(rvalue T) is noexcept(true)
T(lvalue T) is noexcept(true)
U(rvalue U) is noexcept(false)
U(lvalue U) is noexcept(false)
V(rvalue V) is noexcept(true)
V(lvalue V) is noexcept(false)

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR Applied to Behavior as published Correct behavior
CWG 2722 C++17 一時的な実体化が expression がprvalueの場合に
適用されるかどうかが不明確であった
この場合に適用される
CWG 2792 C++11 noexcept 演算子は未定義動作に遭遇した場合に例外が
スローされる可能性があるかどうかを判定する必要があった
必要ない

関連項目

noexcept 指定子 (C++11) 関数が例外をスローする可能性があるかどうかを指定する
動的例外指定 (C++17まで) 関数によってスローされる例外を指定する (C++11で非推奨)