noexcept operator (C++11 feature)

From Cppreference

Jump to: navigation, search

Checks whether the evaluation of an expression can throw an exception

Used within noexcept specifier when the fact if the function can throw depends on template parameters.

Contents

[edit] Syntax

noexcept( expression )

Returns an object of type bool.

[edit] 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, which does not have non-throwing exception specification, unless it is a constant expression.
  • throw expression
  • dynamic_cast expression when the conversion needs a run time check
  • typeid expression when argument type is polymorphic class type

On all other cases the result is true

[edit] Keywords

noexcept

[edit] Example

template <typename T>
void self_assign(T& t) noexcept(noexcept(T::operator=)) 
{ // self_assign is noexcept if and only if T::operator= is noexcept
    t = t;
}

[edit] See also