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

What is point in passing a function call expression into noexcept?

What is the difference between this: bool foo(int){return 0;} void bar()noexcept(foo){} And void bar()noexcept(foo(10)){} In the first bar has the same exception specification as foo and the latter may throw so the two are equivalent to…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
3
votes
0 answers

Why my compiler doesn't allow overriding a deleted non-throwing virtual member function as a deleted throwing member function?

I've read from C++ primer 5th ed. That a virtual member function that won't throw (noexcept) must be overriden as non-throwing function. The exception is if the virtual member function is defined as a 'deleted' member. So I've tried this: struct…
Maestro
  • 2,512
  • 9
  • 24
3
votes
2 answers

Why those two std::string find() functions are declared as noexcept?

According to [string.find#1.2] and [string.find#1.3]: Each member function of the form constexpr size_type F(const charT* s, size_type pos) const; has effects equivalent to: return F(basic_­string_­view(s), pos); Each member…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
3
votes
1 answer

Implicitly deleted special functions with std::chrono::time_point

I have some code like this #include using std::chrono::steady_clock; using std::chrono::time_point; class MyClass { public: MyClass() noexcept = default; MyClass(MyClass const&) noexcept = default; MyClass(MyClass&&) noexcept =…
Eternal
  • 2,648
  • 2
  • 15
  • 21
3
votes
1 answer

Should function-try-block of d'tor allow handling of throwing member-variable d'tor?

I have a class whose destructor is noexcept(false). I know it only throws under certain circumstances, and I want to use it as a member variable of a class with a noexcept destructor. From…
Ben
  • 9,184
  • 1
  • 43
  • 56
3
votes
0 answers

Why does std::function cause the stack to be unwound only when an exception escapes the current function?

I am trying to work around this std::thread bug, which causes the stack to be unwound in an older version of gcc, by applying the noexcept annotation: #include #include void thrower() { throw std::runtime_error("A…
jszopi
  • 31
  • 2
3
votes
2 answers

Should I declare all members/function that doesn't throw noexcept?

One of the CppCoreGuidelines is E.12: Use noexcept when exiting a function because of a throw is impossible or unacceptable. Does it mean that I should declare noexcept on every member and functions that don't throw exceptions and doesn't call other…
DoehJohn
  • 201
  • 1
  • 8
3
votes
1 answer

Why immediate functions are not noexcept by default and why are they allowed to be noexcept(false)?

As of c++20 we can define immediate functions by using the consteval specifier. When a function is declared consteval every call to that function must produce a compile-time constant otherwise the program is ill-formed. Also, since c++20 try-catch…
user7769147
  • 1,559
  • 9
  • 15
3
votes
1 answer

How do I statically assert that a static_cast is noexcept?

I have a functor to perform static casts from any type to a specific type, defined in this way: template struct cast_to { template T_Out operator()(T_In&& value) const noexcept { return…
Giovanni Cerretani
  • 1,693
  • 1
  • 16
  • 30
3
votes
1 answer

Implicit move constructor shall be noexcept if possible

Basically per the standard: An inheriting constructor (12.9) and an implicitly declared special member function (Clause 12) have an exception-specification. If f is an inheriting constructor or an implicitly declared default constructor, copy…
Quest
  • 2,764
  • 1
  • 22
  • 44
3
votes
2 answers

Should a function which can fail only due to integer overflow be noexcept?

The full question fits the title. Here's a quick example: constexpr int increment(int const value) /*noexcept?*/ { return value + 1; } As far as I know, noexcept should be interpreted as "nofail" when deciding whether to mark a function with it…
passing_through
  • 1,778
  • 12
  • 24
3
votes
1 answer

Why compilers fail to detect a 'noexcept' usage mismatch?

Below is some code where the compilers partially detect mismatch between the method declaration (i.e. with 'noexcept' specifier) and the method implementation. The compilers report a warning for the method "bazExcept()" but fail to report anything…
3
votes
1 answer

How to detect a noexcept method using SFINAE

I'm asking about a variation of a (popular) question - detecting the existence of a method of a class. I've read many answers here in SO, and most of the (post C++17) solutions look like this: #include template struct…
David Haim
  • 25,446
  • 3
  • 44
  • 78
3
votes
1 answer

Is it an ODR violation to have inconsistent noexcept in declaration?

This is a two part question, the first regarding something written entirely in C++, the second part regarding the interaction between functions written in C but called from C++. Part 1 Is it an ODR or other violation to have different translation…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
3
votes
2 answers

can std::accumulate throw?

The c++ reference for std::accumulate does not mention any exception to be possibly thrown by std::accumulate, still its definition does not contain noexcept. Assuming one uses types and operations which do not throw, is safe to employ…
francesco
  • 7,189
  • 7
  • 22
  • 49