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
709
votes
9 answers

When should I really use noexcept?

The noexcept keyword can be appropriately applied to many function signatures, but I am unsure as to when I should consider using it in practice. Based on what I have read so far, the last-minute addition of noexcept seems to address some important…
void-pointer
  • 14,247
  • 11
  • 43
  • 61
113
votes
3 answers

What is the difference between C++03 `throw()` specifier and C++11 `noexcept`?

Is there any difference between throw() and noexcept other than being checked at runtime and compile time respectively? This Wikipedia C++11 article suggests that the C++03 throw specifiers are deprecated. Why so ... Is the noexcept capable enough…
iammilind
  • 68,093
  • 33
  • 169
  • 336
53
votes
3 answers

Why vector access operators are not specified as noexcept?

Why std::vector's operator[], front and back member functions are not specified as noexcept?
lizarisk
  • 7,562
  • 10
  • 46
  • 70
48
votes
11 answers

How to deal with noexcept in Visual Studio

I'm trying to create a custom exception that derives from std::exception and overrides what(). At first, I wrote it like this: class UserException : public std::exception { private: const std::string message; public: UserException(const…
svick
  • 236,525
  • 50
  • 385
  • 514
47
votes
4 answers

noexcept, stack unwinding and performance

The following draft from Scott Meyers new C++11 book says(page 2, lines 7-21) The difference between unwinding the call stack and possibly unwinding it has a surprisingly large impact on code generation. In a noexcept function, optimizers …
Pradhan
  • 16,391
  • 3
  • 44
  • 59
46
votes
4 answers

How do I write an ADL-enabled trailing return type, or noexcept specification?

Imagine I'm writing some container template or something. And the time comes to specialize std::swap for it. As a good citizen, I'll enable ADL by doing something like this: template void swap(my_template& x, my_template& y) { …
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
45
votes
4 answers

Does constexpr imply noexcept?

Does constexpr specifier imply noexcept specifier for a function? Answer to the similar question says "yes" concerning inline specifier, but Eric Niebler's article makes me wonder about possible answer to the current one. On my mind answer can…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
44
votes
1 answer

Passing null pointer to placement new

The default placement new operator is declared in 18.6 [support.dynamic] ¶1 with a non-throwing exception-specification: void* operator new (std::size_t size, void* ptr) noexcept; This function does nothing except return ptr; so it is reasonable…
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
41
votes
1 answer

`static constexpr` function called in a constant expression is...an error?

I have the following code: class MyClass { static constexpr bool foo() { return true; } void bar() noexcept(foo()) { } }; I would expect that since foo() is a static constexpr function, and since it's defined before bar is declared, this…
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
40
votes
1 answer

Is there an automatic noexcept specifier?

I've heard that noexcept keyword is more like 'it should never throw an exception' rather than 'it doesn't'. I don't think it's good to use noexcept keyword if I'm not sure it throws an exception or not, but noexcept keyword is sometimes related to…
Inbae Jeong
  • 4,053
  • 25
  • 38
38
votes
1 answer

std::function with noexcept in C++17

In C++17 noexcept has been added to the type system: void r1( void (*f)() noexcept ) { f(); } void foo() { throw 1; } int main() { r1(foo); } The latest versions of GCC and Clang in C++17 mode reject the call r1(foo), because void (*)() cannot…
M.M
  • 138,810
  • 21
  • 208
  • 365
38
votes
3 answers

Does adding `noexcept(false)` benefit the code in any way?

Recently in my code I have been explicitly writing noexcept(false) on functions that I know do throw exceptions, mainly for people reading the code. However, I am wondering if this affects the behavior of my code or the way the compiler interprets…
LB--
  • 2,506
  • 1
  • 38
  • 76
37
votes
1 answer

Destructors and noexcept

I am a little bit confused with destructors and noexcept. My understanding was that in C++11 any destructor, including user-defined, is implicitly noexcept(true), even if we throw from it. And one has to specify explicitly noexcept(false) if they…
lapk
  • 3,838
  • 1
  • 23
  • 28
35
votes
4 answers

Handling gcc's noexcept-type warning

Consider this example, from bug 80985: template void call(Func f) { f(); } void func() noexcept { } int main() { call(func); } Compiling this with all warnings enabled, as you do, yields: $ g++ -std=c++14 -Wall foo.cxx…
Barry
  • 286,269
  • 29
  • 621
  • 977
34
votes
1 answer

C++ ISO noexcept of noexcept

In the C++ standard there is the following definition: template void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b))); What does noexcept(noexcept(swap(*a, *b))) do?
yonutix
  • 1,964
  • 1
  • 22
  • 51
1
2 3
22 23