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

Why is default noexcept move constructor being accepted?

Assume the following c++17 code: #include namespace dtl { struct One { explicit One(int); ~One() = default; One(const One &) = delete; auto operator=(const One &) -> One & = delete; auto…
JVApen
  • 11,008
  • 5
  • 31
  • 67
8
votes
1 answer

`noexcept` behavior of `constexpr` functions

The wording of [expr.unary.noexcept] changed in C++17. Previously (n4140, 5.3.7 noexcept operator [expr.unary.noexcept]), my emphasis: The result of the noexcept operator is false if in a potentially-evaluated context the expression would…
Amir Kirsh
  • 12,564
  • 41
  • 74
8
votes
1 answer

noexcept visitation for std::variant

For some standard library classes, access to parts of their contents may legitimately fail. Usually you have the choice between some potentially throwing method an one that is marked noexcept. The latter spares the check on the precondition, so if…
Tobi
  • 2,591
  • 15
  • 34
8
votes
1 answer

Evaluating noexcept specifier before template type deduction

Please see the following code: #include struct A { A(int, int) {} }; struct tag {}; template struct is_noexcept { static constexpr bool value = noexcept(A{std::declval()...}); }; struct B : A { //#1 …
Junekey Jeon
  • 1,496
  • 1
  • 11
  • 18
8
votes
3 answers

can floating point multiplication throw an exception in C++?

Is this possible? I don't think it is, but I don't know if this is something the standard would say, or if it's implementation defined? I'm asking because I'm wondering whether it's safe or worth it to mark a constexpr function like this…
Peter Mitrano
  • 2,132
  • 28
  • 31
8
votes
1 answer

Can the std::vector default constructor throw an exception

If I construct an empty std::vector using the default constructor (and the default allocator), can it throw an exception? In general, allocating space for the elements of a container can throw an exception (which would be a std::bad_alloc). But the…
Raedwald
  • 46,613
  • 43
  • 151
  • 237
8
votes
2 answers

“Default member initializer needed within definition of enclosing class outside of member functions” - is my code ill-formed?

struct foo { struct bar { ~bar() {} // no error w/o this line }; bar *data = nullptr; // no error w/o this line foo() noexcept = default; // no error w/o this line }; Yes, I know, there is another…
Walter
  • 44,150
  • 20
  • 113
  • 196
8
votes
2 answers

C++ noexcept declaration changes template deduction

I was tinkering to confirm the example on page 91 of Effective Modern C++, and I ran into what seems to be a strange issue. This code template void doStuff(C& a, C& b) noexcept(noexcept(doStuff(a.front(), b.front()))) { std::cout <<…
piyo
  • 461
  • 1
  • 5
  • 18
8
votes
1 answer

Why is there no `noexcept` specifier on std::unordered_map::count?

I was reading C++ reference page about std::unordered_map. The empty and size methods are noexcept qualified, but not count. I don't think it should throw in count. Am I missing something?
Richard Dally
  • 1,432
  • 2
  • 21
  • 38
8
votes
2 answers

Can a function marked as noexcept have exceptions inside?

Let's say that I have a function marked as noexcept but there's a line of code inside that can throw. That line of code will be in a try block and the exception will be caught. Does that cause anything? void MyFunc() noexcept { try { …
SLC
  • 2,167
  • 2
  • 28
  • 46
8
votes
1 answer

Rationale for std::move_if_noexcept still moving throwing move-only types?

move_if_noexcept will: return an rvalue -- facilitating a move -- if the move constructor is noexcept or if there is no copy constructor (move-only type) return an lvalue -- forcing a copy -- otherwise I found this rather surprising, as a…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
8
votes
1 answer

noexcept depend on noexcept of a member function

Consider: class test { private: int n; int impl () const noexcept { return n; } public: test () = delete; test (int n) noexcept : n(n) { } int get () const…
7
votes
2 answers

C++17: Deducing function noexcept specifier as non-type parameter

I've noticed that MSVC sometimes fails to deduce non-type parameters that other compilers accept, and recently came upon a simple example involving the function noexcept specifier (which is part of the function's signature since C++17): template…
Jack Harwood
  • 348
  • 2
  • 7
7
votes
1 answer

Ternary operator applied to different lambdas produces inconsistent results

Consider the following which uses the ternary operator to get the common function pointer type of the two lambdas int main() { true ? [](auto) noexcept {} : [](int) {}; } GCC-trunk only accepts it in C++14 but rejects it in C++17/20 with…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
7
votes
1 answer

Can a non-throwing function pointer point to a throwing-function?

On C++ Primer on noexcept exception specification, it is said that a pointer to a function that may throw Implicitly (defined without exception specification e.g: void(*p)();) or explicitly (void(*p)() noexcept(false);) may point to any function…
Maestro
  • 2,512
  • 9
  • 24