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

Der noexcept -Operator führt eine Überprüfung zur Kompilierzeit durch, die true zurückgibt, wenn ein Ausdruck deklariert ist, keine Ausnahmen auszulösen.

Es kann innerhalb des noexcept Specifiers einer Funktionsvorlage verwendet werden, um zu deklarieren, dass die Funktion für einige Typen Ausnahmen auslöst, für andere jedoch nicht.

Inhaltsverzeichnis

Syntax

noexcept( Ausdruck )

Gibt einen prvalue vom Typ bool zurück. Das Ergebnis ist true wenn die Menge der potenziellen Ausnahmen des Ausdrucks leer ist (bis C++17) Ausdrucks als nicht-werfend spezifiziert ist (seit C++17) , und false andernfalls.

expression ist ein unevaluated operand .

Wenn expression ein Prvalue ist, wird temporary materialization angewendet.

(since C++17)

Hinweise

Selbst wenn noexcept ( expr ) true ist, kann eine Auswertung von expr dennoch eine Exception werfen, wenn undefiniertes Verhalten auftritt.

Wenn expression von einem Klassentyp oder (möglicherweise mehrdimensionalem) Array davon ist, erfordert die temporäre Materialisierung, dass der Destruktor nicht gelöscht und zugänglich ist.

(since C++17)

Schlüsselwörter

noexcept

Beispiel

#include <iostream>
#include <utility>
#include <vector>
void may_throw();
void no_throw() noexcept;
auto lmay_throw = []{};
auto lno_throw = []() noexcept {};
class T
{
public:
    ~T(){} // dtor prevents move ctor
           // copy ctor is noexcept
};
class U
{
public:
    ~U(){} // dtor prevents move ctor
           // copy ctor is 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"
        // note: the following tests also require that ~T() is noexcept because
        // the expression within noexcept constructs and destroys a temporary
        "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";
}

Ausgabe:

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)

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrektes Verhalten
CWG 2722 C++17 es war unklar, ob temporäre Materialisierung
angewendet wird, wenn expression ein Prvalue ist
sie wird
in diesem Fall angewendet
CWG 2792 C++11 der noexcept Operator musste bestimmen, ob Ausnahmen
geworfen werden können im Fall von undefiniertem Verhalten
nicht erforderlich

Siehe auch

noexcept Spezifizierer (C++11) gibt an, ob eine Funktion Ausnahmen werfen könnte
Dynamische Ausnahmespezifikation (bis C++17) gibt an, welche Ausnahmen von einer Funktion geworfen werden (veraltet in C++11)