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
21
votes
5 answers

Should a theoretically, but not practically, throwing function be declared noexcept?

Is it safe to declare the following function noexcept even though v.at(idx) could theoretically throw a out_of_range exception, but practically not due to the bounds check? int get_value_or_default(const std::vector& v, size_t idx) noexcept { …
j00hi
  • 5,420
  • 3
  • 45
  • 82
21
votes
1 answer

C++11, `noexcept` specifier, definition versus declaration

If a declared function has a noexcept specificator (noexcept, noexcept(true), noexcept(false), or any other noexcept(expr) which evaluates to true or false), but it's defined in another place, do I need to specify the noexcept specifier in the…
ABu
  • 10,423
  • 6
  • 52
  • 103
20
votes
1 answer

Why is swapping multidimensional arrays not noexcept?

I have the following snippet: #include #include int main(int argc, char** argv) { int x[2][3]; int y[2][3]; using std::swap; std::cout << noexcept(swap(x, y)) << "\n"; return 0; } Using GCC 4.9.0, this…
orlp
  • 112,504
  • 36
  • 218
  • 315
19
votes
2 answers

=default in declaration vs definition

I know that instead of writing: class A { public: A(A&&) noexcept = default; }; One should better write class A { public: A(A&&) noexcept; }; inline A::A(A&&) noexcept = default; The reasons I've heard are: It avoids the constructor…
laike9m
  • 18,344
  • 20
  • 107
  • 140
19
votes
4 answers

Determine whether a constructor of an abstract base class is noexcept?

In C++11 and later, how to determine whether a constructor of an abstract base class is noexcept? The following methods don't work: #include #include #include struct Base { Base() noexcept; virtual int f() = 0; }; //…
jotik
  • 17,044
  • 13
  • 58
  • 123
19
votes
4 answers

How do I create an alias for a noexcept function pointer?

I'd like to do this: using function_type = void (*)(void*)noexcept; But I get an error "exception specifications are not allowed in type aliases." (clang in version 6.1 of Xcode) Is there a workaround to create an alias with a noexcept…
Michael Gazonda
  • 2,720
  • 1
  • 17
  • 33
19
votes
2 answers

Does it make sense to declare inline functions noexcept?

From what I can tell, the SO community is divided on whether declaring a function noexcept enables meaningful compiler optimizations that would not otherwise be possible. (I'm talking specifically about compiler optimizations, not library…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
19
votes
2 answers

Use of the noexcept specifier in function declaration and definition?

Consider the following function: // Declaration in the .h file class MyClass { template void function(T&& x) const; }; // Definition in the .cpp file template void MyClass::function(T&& x) const; I want to make this…
Vincent
  • 57,703
  • 61
  • 205
  • 388
19
votes
2 answers

How to use noexcept in assignment operator with copy-and-swap idiom?

The move assignment operator should often be declared noexcept (i.e. to store the type in STL containers). But the copy-and-swap idiom allows both copy- and move- assignment operators to be defined in a single piece of code. What to do with noexcept…
lizarisk
  • 7,562
  • 10
  • 46
  • 70
18
votes
0 answers

Noexcept-specification in the std::vector's swap function

I noticed that the std::vector container's swap function has a different noexcept-specification than all other containers. Specifically, the function is noexcept if the expression std::allocator_traits::propagate_on_container_swap ||…
LoS
  • 448
  • 1
  • 3
  • 15
18
votes
2 answers

C++ omitting `noexcept` specifier versus `noexcept(false)`, what is their precise meaning?

If I mark a function as noexcept(false), or any other expression which evaluates to false, what does it means? (1) am I ensuring to the compiler that the function can throw an exception?, (2) or am I ensuring nothing about whether it can throw…
ABu
  • 10,423
  • 6
  • 52
  • 103
17
votes
1 answer

noexcept specifiers in function typedefs

Are noexcept specifiers accepted in function typedefs? as in: typedef void (*fptr)() noexcept; Intuitively, noexcept specifiers seem to make sense since they would allow some optimisations at the caller's side. I got a mixed answer from gcc…
mirk
  • 5,302
  • 3
  • 32
  • 49
17
votes
1 answer

Strange behavior when a function declared as noexcept throws an exception in its default argument

Here is an example about my question: struct B { B(B&&, int = (throw 0, 0)) noexcept {} }; I know this is a very strange piece of code. It is just used to illustrate the problem. The move constructor of B has a noexcept specifier, while it has…
Pluto
  • 910
  • 3
  • 11
17
votes
1 answer

noexcept specifier mysteriously breaks compilation (clang, gcc disagree)

The code in question is #include #include template void for_each(F&&) noexcept {} template void for_each(F&& f, T&& v, Us&&... us) { std::invoke(std::forward(f),…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
17
votes
1 answer

What are the rules for noexcept on default defined move constructors?

Especially in connection with std::vector it is important that types are noexcept movable when possible. So when declaring a move constructor = default like in struct Object1 { Object1(Object1 &&other) =…
mfuchs
  • 2,190
  • 12
  • 20
1 2
3
22 23