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
16
votes
1 answer

Why are deque's pop_front() and pop_back() not noexcept?

Is there any reason that std::deque's pop_front() and pop_back() are not noexcept in C++11 and higher or was that just forgotten?
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
16
votes
1 answer

Are C++ standard library implementations allowed to strengthen noexcept specifications?

According to the C++ standard, are implementations of the C++ standard library allowed to strengthen noexcept specifications of methods and other functions of the C++ standard library as defined by the standard? For example, if the C++ standard…
jotik
  • 17,044
  • 13
  • 58
  • 123
16
votes
1 answer

g++-4.8.1 thinks that an explicitly-declared destructor with no exception specification is always noexcept(true)

Consider the following program: #include struct Thrower { ~Thrower() noexcept(false) { throw 1; } }; struct Implicit { Thrower t; }; static_assert(!std::is_nothrow_destructible::value, "Implicit"); struct…
Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
15
votes
1 answer

How to static cast throwing function pointer to noexcept in C++17?

C++17 makes noexcept part of a function's type. It also allows implicit conversions from noexcept function pointers to potentially throwing function pointers. void (*ptr_to_noexcept)() noexcept = nullptr; void (*ptr_to_throwing)() = ptr_to_noexcept;…
Filipp
  • 1,843
  • 12
  • 26
15
votes
3 answers

C++ noexcept for a function not throwing exceptions, but can cause a memory failure

For example, it's pretty common to have two separate ways to access elements of a private array, overloading the array subscripting operator, or defining at: T& operator[](size_t i) { return v[i]; } T const& operator[](size_t i) const { return v[i];…
ABu
  • 10,423
  • 6
  • 52
  • 103
15
votes
2 answers

Static analysis of noexcept "violations" in C++

I'm trying to write exception safe code. I find that using C++11's noexcept specifier makes this goal a whole lot more achievable. The general idea, of course, is that a function should be marked as 'noexcept' if, and only if all the functions that…
Kristian Spangsege
  • 2,903
  • 1
  • 20
  • 43
14
votes
1 answer

"noexcept" vs "Throws: nothing"

While going through the last edits of the C++0x Working draft I found a lot of removal of the keyword noexcept addition of textual Throws: nothing at the same place and vice versa. Just some examples: replacement of noexcept against Throws:…
towi
  • 21,587
  • 28
  • 106
  • 187
14
votes
1 answer

Does adding noexcept break binary compatibility?

Simple question: If change this: void someMethod(); to void someMethod() noexcept; will it break binary compatibility, or does the method signature remain the same?
Felix
  • 6,885
  • 1
  • 29
  • 54
14
votes
1 answer

Is there any point in declaring a deleted function as noexcept?

Consider these two possible definitions for a class: Exhibit A: struct A { A() = delete; }; Exhibit A′: struct A { A() noexcept = delete; } Is there any point in declaring a deleted function as noexcept?
user2296177
  • 2,807
  • 1
  • 15
  • 26
13
votes
2 answers

Is my code ill-formed if I intentionally mark a function [that I know may probably throw] noexcept to terminate immediately in case of exception?

I know, marking a function noexcept may be useful in order to get many awesome optimizations [in some cases], such as move semantics, for example. But assume, I have a function in my code that does very critical stuff, and, if this function fails,…
Alexey104
  • 969
  • 1
  • 5
  • 17
13
votes
4 answers

Can a noexcept function still call a function that throws in C++17?

In P0012R1, "Make exception specifications be part of the type system", I see that noexcept is now becoming a part of the function type. I can't tell whether this will prevent noexcept(true) functions from still being able to call noexcept(false)…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
13
votes
1 answer

What algorithms and containers in the C++ 11 STL can go faster if a type is noexcept?

A debate came up at work regarding how much to care about using noexcept. We all know that noexcept doesn't really do a huge amount for the compiler's optimiser except for externally defined code which the compiler otherwise has to assume can throw…
Niall Douglas
  • 9,212
  • 2
  • 44
  • 54
13
votes
2 answers

Why are the swap member functions in STL containers not declared noexcept?

As of N3797 the C++ standard requires swap functions of containers to not throw any exceptions unless specified otherwise [container.requirements.general] (23.2.1§10). Why are the swap member functions that are specified to not throw not declared…
Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
13
votes
4 answers

Should I declare the copy constructor of my exceptions noexcept?

In More Effective C++, Scott Meyers says C++ specifies that an object thrown as an exception is copied. I suppose then, that if the copy constructor throws an exception in turn, std::terminate is called, so this is a good reason for declaring all…
qdii
  • 12,505
  • 10
  • 59
  • 116
13
votes
2 answers

Never annotate functions involving dynamic memory allocation as noexcept?

Assume you have a function that normally can never fail, for example: std::string convert_integer_to_string(int x); In pricipal, this would be a candidate for noexcept. However, the implementation most likely involves involves dynamic memory…
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239