Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/noexcept"

From cppreference.com
< cpp‎ | language
m (Text replace - "cpp/keywords/" to "cpp/keyword/")
m (Shorten template names. Use {{lc}} where appropriate.)
Line 8: Line 8:
 
===Syntax===
 
===Syntax===
  
{{sdcl list begin}}
+
{{sdsc begin}}
{{sdcl list item | {{ttb|noexcept(}} {{sparam|expression}} {{ttb|)}}}}
+
{{sdsc | {{ttb|noexcept(}} {{spar|expression}} {{ttb|)}}}}
{{sdcl list end}}
+
{{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 {{sparam|expression}}. The result is {{ttb|false}} if the {{sparam|expression}} contains at least one of the following potentially evaluated constructs:
+
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===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/language/dcl list noexcept_spec}}
+
{{dsc inc | cpp/language/dcl list noexcept_spec}}
{{dcl list template | cpp/language/dcl list except_spec}}
+
{{dsc inc | cpp/language/dcl list except_spec}}
{{dcl list end}}
+
{{dsc end}}
  
 
[[de:cpp/language/noexcept]]
 
[[de:cpp/language/noexcept]]

Revision as of 19:05, 31 May 2013

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
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
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 

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

noexcept

Example

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

See also

Template:cpp/language/dcl list noexcept specTemplate:cpp/language/dcl list except spec