Questions tagged [noexcept]

A C++ keyword used for exception-specifications and to query whether an expression can throw exceptions

When used at the end of a function declaration noexcept(constant)opt indicates whether or not a function can throw exceptions, replacing the deprecated C++03 form of exception specification using throw(…). If the constant expression in parentheses evaluates to true (or if the parentheses are omitted) it indicates the function will not throw, otherwise it indicates it might throw.

When used as an operator noexcept( expr ) does not evaluate the expression but returns a boolean indicating whether the expression cannot throw exceptions.

Use this tag for questions about either use of the noexcept keyword.

For the history and rationale of noexcept see Using noexcept

331 questions
33
votes
4 answers

Why is std::unique_ptr::reset() always noexcept?

A recent question (and especially my answer to it) made me wonder: In C++11 (and newer standards), destructors are always implicitly noexcept, unless specified otherwise (i.e. noexcept(false)). In that case, these destructors may legally throw…
anderas
  • 5,744
  • 30
  • 49
33
votes
2 answers

Noexcept and copy, move constructors

Everywhere I look it seems to be the agreement that the standard library must call copy constructors instead of move constructors when the move constructor is noexcept(false). Now I do not understand why this is the case. And futher more Visual…
Generic Name
  • 1,083
  • 1
  • 12
  • 19
32
votes
2 answers

What is noexcept useful for?

I saw that C++ 11 added the noexcept keyword. But I don't really understand why is it useful. If the function throws when it's not supposed to throw - why would I want the program to crash? So when should I use it? Also, how will it work along with…
user972014
  • 3,296
  • 6
  • 49
  • 89
29
votes
5 answers

Does the C++ standard mandate that C-linkage functions are `noexcept`?

I can't find anything in the standard that forces functions declared with extern "C" to be noexcept, either implicitly or explicitly. Yet, it should be clear that C calling conventions cannot support exceptions... or is it? Does the standard mention…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
28
votes
2 answers

Why is my defaulted move constructor not noexcept?

According to the answer of this question, a default move constructor can be defined as noexcept under certain conditions. For instance, the following class generates a noexcept move constructor: class C {}; According to the answer to this question,…
Pierre
  • 1,942
  • 3
  • 23
  • 43
28
votes
2 answers

Can the "main" function be declared with the "noexcept" specifier?

Is the following code valid in C++? int main() noexcept { } Both clang++ 3.8.0 and g++ 7.2.0 compile it fine (with -std=c++14 -O0 -Wall -Wextra -Werror -pedantic-errors compilation flags). Is it allowed to use complex conditions (e.g. including…
Constructor
  • 7,273
  • 2
  • 24
  • 66
28
votes
3 answers

How to use noexcept in C++ or How does it work?

I am unable to understand the use & purpose of the noexcept keyword in C++11/14. I understand that it is a signature for a function that does not emit exceptions. But does it really work? Look at this code below : #include #include…
Ankit Acharya
  • 2,833
  • 3
  • 18
  • 29
27
votes
2 answers

Using noexcept as a lambda modifier or parameter constraint

Can the noexcept modifier be applied to a lambda expression? If so, how? Can noexcept be made a constraint on a function argument? For example, something like in the following code, where the meaning is that the callback function must be…
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
24
votes
2 answers

Are move constructors required to be noexcept?

I've been reading some contradicting articles in regards whether move constructors/assignment is allowed to throw or not. Therefore I'd like to ask whether move constructors/assignments are allowed to throw in the final C++11 standard?
ronag
  • 49,529
  • 25
  • 126
  • 221
24
votes
1 answer

Why are std::array::front and std::array::back not noexcept?

I'm new to the use of the noexcept specifier and I do not understand why std::array::front and std::array::back are not declared noexcept (whereas std::array::begin and std::array::end are). What is the reason of that?
Vincent
  • 57,703
  • 61
  • 205
  • 388
22
votes
1 answer

noexcept specifier with default arguments construction

Take the following example code: void test(const Item& item = Item()) { ... } Assume that, once item has been passed to the function, this cannot throw. The question is: the function should be marked noexcept or noexcept(noexcept(Item()))? IHMO,…
dodomorandi
  • 1,143
  • 1
  • 11
  • 18
22
votes
1 answer

How will C++17 exception specifier type system work?

Studying about "noexcept specifier(and operator)", I wrote a simple code. And I am surprised that this piece of code: void asdf() noexcept {} int main() { auto f = asdf; std::cout << std::boolalpha << noexcept(f()) << std::endl; } prints…
suhdonghwi
  • 955
  • 1
  • 7
  • 20
22
votes
2 answers

Why is it allowed to throw an exception inside a noexcept-tagged function?

I'm having a hard time understanding this. double compute(double x, double y) noexcept { if (y == 0) throw std::domain_error("y is zero"); return x / y; } this compiles fine in clang (I haven't checked gcc), but it seems nonsense to…
tunnuz
  • 23,338
  • 31
  • 90
  • 128
22
votes
1 answer

Program with "noexcept" constructor accepted by gcc, rejected by clang

The code: struct T { T() {} }; struct S { T t; S() noexcept = default; }; int main() { // S s; } g++ 4.9.2 accepts this with no errors or warnings, however clang 3.6 and 3.7 report for line 7: error: exception specification of…
M.M
  • 138,810
  • 21
  • 208
  • 365
21
votes
1 answer

Constructor with by-value parameter & noexcept

In this example code: explicit MyClass(std::wstring text) noexcept; Is the use of noexcept here correct? wstring can potentially throw on construction but does the throw happen before we are in the constructor or while we are in the…
user802003
1
2
3
22 23