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

Is noexcept useless when not throwing is zero-cost?

Is the noexcept specifier useless if your implementation has a zero-cost (if nothing is thrown) exception model? What is an example where lacking noexcept has a consequence?
j__
  • 632
  • 4
  • 18
12
votes
1 answer

In what cases does a C++ compiler infer noexcept?

Suppose a C++ compiler is compiling a function whose definition is available in the same translation unit as its invocation. Suppose that it does not throw itself nor calls a function that is know to throw. Suppose also no extern C code is called,…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
12
votes
1 answer

noexcept, inheriting constructors and the invalid use of an incomplete type that is actually complete

I'm not sure if it's a bug of the GCC compiler or the intended behavior of noexcept. Consider the following example: struct B { B(int) noexcept { } virtual void f() = 0; }; struct D: public B { using B::B; D()…
skypjack
  • 49,335
  • 19
  • 95
  • 187
12
votes
1 answer

Why std::map find() is not declared as noexcept?

C++14 standard defines the find() member functions of std::map as follows: iterator find(const key_type& x); const_iterator find(const key_type& x) const; Why are these functions not defined as noexcept? What could possibly go wrong inside, that…
PowerGamer
  • 2,106
  • 2
  • 17
  • 33
12
votes
1 answer

Why there is no std::move_if_noexcept counterpart for std::forward in C++11/14?

I have watched Scott Meyers' talk on GoingNative2013 "An Effective C++11/14 Sampler" and he explained the use of std::move_if_noexcept. So in my opinion there should be a std::forward_if_noexcept to also guarantee exception-safety for forward? Why…
12
votes
1 answer

C++11 noexcept qualifier and inline methods

Does C++11 give any guarantees about inline functions or methods, when they make calls to other functions declared with the noexcept qualifier? class My_String { ... const char * c_str () const noexcept; inline operator const char * ()…
Brett Hale
  • 21,653
  • 2
  • 61
  • 90
11
votes
1 answer

How can I say "noexcept if execution of protected base constructor is noexcept"?

We had this situation and wondered about the best way to fix it template struct A : T { A(T &&t) noexcept(noexcept(T(std::move(t)))) :T(std::move(t)) { } }; This unfortunately fails to compile because T's move constructor is…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
11
votes
2 answers

What does noexcept exactly encompass for constructors?

According to the C++ standard, what exactly does a noexcept noexcept-specification on a class constructor apply to? the function-body? initialization of members in the optional ctor-initializer? initialization of base classes in the optional…
jotik
  • 17,044
  • 13
  • 58
  • 123
11
votes
1 answer

Enforce "noexcept" on std::function?

This code compiles and runs, throwing the int: #include void r( std::function f ) { f(); } void foo() { throw 1; } int main() { r(foo); } However I would like the compiler to reject the line r(foo); because r…
M.M
  • 138,810
  • 21
  • 208
  • 365
11
votes
1 answer

function-try-block and noexcept

For the following code struct X { int x; X() noexcept try : x(0) { } catch(...) { } }; Visual studio 14 CTP issues the warning warning C4297: 'X::X': function assumed not to throw an exception but does note:…
a.lasram
  • 4,371
  • 1
  • 16
  • 24
11
votes
1 answer

Should constructors on std::chrono::...::time_point be noexcept? (Or why aren't they?)

I encountered this problem trying to hold on to an atomic time_point, see (atomic requires noexcept default constructor if default constructor available): http://cplusplus.github.io/LWG/lwg-active.html#2165 The simple problem is std::atomic or…
amurray
  • 341
  • 2
  • 7
10
votes
1 answer

Are `inline` and `noexcept` redundant in a consteval context?

I am working with some code in which constexpr functions are used which I currently refactor to be consteval whenever possible. inline constexpr auto example() noexcept { /*...*/ } As I understand it, the inline keyword above is already redundant…
mutableVoid
  • 1,284
  • 2
  • 11
  • 29
10
votes
1 answer

Why there is no std::uninitialized_move_if_noexcept?

C++17 adds std::uninitialized_move, but there is no std::uninitialized_move_if_noexcept that would use std::move_if_noexcept internally. In my opinion, it would be useful, since now, if we want to reallocate, we still need to write something as if…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
10
votes
1 answer

`std::terminate` invocation in a `noexcept` function with limited visibility - gcc vs clang codegen

Consider the following code snippet: void f(); void a() { f(); } void b() noexcept { f(); } In the scenario above, the body of f is not visible to the compiler in the current translation unit. Therefore, since b is marked noexcept,…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
10
votes
2 answers

Add noexcept on otherwise-non-throwing inline functions calling C functions?

I've implemented C++ bindings for some C library. The library API calls can fail, but obviously can't throw anything; and my bindings are, for the purposes of this question, all inline. Now, the compiler can figure out, for most of my inlined…
einpoklum
  • 118,144
  • 57
  • 340
  • 684