Difference between revisions of "cpp/language/noexcept"
From cppreference.com
m (Text replace - "cpp/keywords/" to "cpp/keyword/") |
m (Shorten template names. Use {{lc}} where appropriate.) |
||
Line 8: | Line 8: | ||
===Syntax=== | ===Syntax=== | ||
− | {{ | + | {{sdsc begin}} |
− | {{ | + | {{sdsc | {{ttb|noexcept(}} {{spar|expression}} {{ttb|)}}}} |
− | {{ | + | {{sdsc end}} |
Returns an object of type {{c|bool}}. | Returns an object of type {{c|bool}}. | ||
Line 16: | Line 16: | ||
===Explanation=== | ===Explanation=== | ||
− | The {{tt|noexcept}} operator does not evaluate {{ | + | The {{tt|noexcept}} operator does not evaluate {{spar|expression}}. The result is {{ttb|false}} if the {{spar|expression}} contains at least one of the following potentially evaluated constructs: |
:* call to any type of function that does not have non-throwing exception specification, unless it is a {{rlp|constexpr | constant expression}}. | :* call to any type of function that does not have non-throwing exception specification, unless it is a {{rlp|constexpr | constant expression}}. | ||
Line 40: | Line 40: | ||
===See also=== | ===See also=== | ||
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc inc | cpp/language/dcl list noexcept_spec}} |
− | {{ | + | {{dsc inc | cpp/language/dcl list except_spec}} |
− | {{ | + | {{dsc end}} |
[[de:cpp/language/noexcept]] | [[de:cpp/language/noexcept]] |
Revision as of 19:05, 31 May 2013
The noexcept
operator performs a compile-time check that returns true if an expression is declared to not throw any exceptions.
It can be used within a function template's noexcept specifier to declare that the function will throw exceptions for some types but not others.
Contents |
Syntax
noexcept( expression )
|
|||||||||
Returns an object of type bool.
Explanation
The noexcept
operator does not evaluate expression. The result is false
if the expression contains at least one of the following potentially evaluated constructs:
- call to any type of function that does not have non-throwing exception specification, unless it is a constant expression.
-
throw
expression -
dynamic_cast
expression when the target type is a reference type, and conversion needs a run time check -
typeid
expression when argument type is polymorphic class type
In all other cases the result is true
.
Keywords
Example
Run this code
template <class T> void self_assign(T& t) noexcept(noexcept(t = t)) { // self_assign is noexcept if and only if T::operator= is noexcept t = t; }