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

Can we refer to member variables in a noexcept specification?

Please consider the following code snippet: template class vector { public: typename Tuple::size_type size() const noexcept(noexcept(m_elements.size())) { return m_elements.size(); } private: Tuple…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
10
votes
1 answer

Casting a function pointer into a noexcept specified function pointer

Say I have these declarations: using fp_type = void(*)(); using fp2_type = void(*)() noexcept; and void func(){} fp_type fp(func); Is the cast fp2_type(fp) well-formed? The other way around (casting a noexcept-specified function pointer into a…
user1095108
  • 14,119
  • 9
  • 58
  • 116
9
votes
1 answer

Is `this` allowed inside a noexcept specification?

I have some code which requires me to use *this, but I want it to be noexcept friendly: struct foo; // Would actually be something with conditional noexcept void do_something(foo&); struct foo { void fn() …
Justin
  • 24,288
  • 12
  • 92
  • 142
9
votes
2 answers

Point of evaluation of exception specification

Consider these code snippets: Version (1) void q() {} class B { void f() noexcept(noexcept(q())) {q(); } decltype(&B::f) f2; }; Version (2) void q() {} class B { void f() noexcept(true) {q(); } decltype(&B::f) f2; }; Version (3) void q()…
overseas
  • 1,711
  • 1
  • 18
  • 30
9
votes
1 answer

Does anything prevent std::optional::value_or() from being conditionally noexcept?

Here's the definition of value_or() from the C++17 standard: template constexpr T value_or(U&& v) const&; Effects: Equivalent to: return bool(*this) ? **this : static_cast(std::forward(v)); Remarks: If is_copy_constructible_v &&…
knatten
  • 5,191
  • 3
  • 22
  • 31
9
votes
2 answers

Why is unique_ptr operator* not noexcept?

While implementing a basic std library for my hobby OS I came across this and wondered why: Both operator->() and T* get() are marked as noexcept, however operator*() is not. According to the reference it should be equivalent to *get(), which would…
Thalhammer
  • 285
  • 3
  • 7
9
votes
2 answers

Wouldn't it make sense to overload with respect to noexcept?

I am trying to understand the noexcept feature. I know it could be confusing, but besides that could noexcept be deduced from the calling function when possible. This is a non working example of this situation, void f(){} void f() noexcept{} // not…
alfC
  • 14,261
  • 4
  • 67
  • 118
9
votes
1 answer

Inconsistencies with conditional noexcept and overloads

I have an issue which is quite similar to this one. In short, I have a magic method, which is noexcept if another method is noexcept. The weird thing is that this "another method" has two overloads, and the compiler chooses the second overload to…
Dante
  • 404
  • 2
  • 10
9
votes
1 answer

How can I declare a move constructor of a wrapper type X noexcept depending on is_nothrow_move_constructible?

Assume I have a wrapper type template struct X {/*..*/}; and I cannot just X(X&&) = default because I have to do non-trivial stuff there. However, I want it to be noexcept but only in case that T(T&&) is noexcept. This can be tested…
bitmask
  • 32,434
  • 14
  • 99
  • 159
9
votes
1 answer

How do I create a noexcept function pointer?

I'd like to create a function pointer like this: void(*function_pointer)()noexcept; But, this doesn't work. It seems that an exception specifier in a function declaration is invalid. There must be a way to do this though. Right? This was linked to…
Michael Gazonda
  • 2,720
  • 1
  • 17
  • 33
9
votes
1 answer

Can placement new (expression) throw if the constructor of the object is noexcept?

template struct Obj { // Plain Old Data for T using InternalPod = typename std::aligned_storage::value>::type; InternalPod value_pod_; template Obj(Args&&... args) { // my…
bolov
  • 72,283
  • 15
  • 145
  • 224
9
votes
2 answers

Can I mark a classes move-operation noexcept if it contains a standard container?

The idiomatic way to implement move-operations on classes with a standard container member can not be noexcept and therefore will not be movable by operations like vector.push_back(). Or am I mistaken? To get speed from vector data; //…
towi
  • 21,587
  • 28
  • 106
  • 187
9
votes
2 answers

Can I force a default special member function to be noexcept?

The following structure fails to compile under C++11 due to the fact that I have declared the move assignment operator as noexcept: struct foo { std::vector data; foo& operator=(foo&&) noexcept = default; }; The default move assignment…
marack
  • 2,024
  • 22
  • 31
9
votes
1 answer

noexcept specifier and compiler optimizations

I have read unclear things regarding the noexcept specifier and compiler optimizations. When specifying noexcept the compiler may optimize: Compile time (faster compilation). Execution time (code runs faster). Or both? Or none?
talles
  • 14,356
  • 8
  • 45
  • 58
8
votes
1 answer

Is calling a "noexcept function" through a "function" lvalue undefined?

[expr.call]/6: Calling a function through an expression whose function type is different from the function type of the called function's definition results in undefined behavior. void f() noexcept {}; // function type is "noexcept function" void…