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
1
vote
1 answer

Need for std::decay in noexcept operator?

The following code requires using std::decay inside noexcept operator in gcc, but not in clang. template class B; template class B { T t; public: template constexpr B(U&& t) // without…
Amir Kirsh
  • 12,564
  • 41
  • 74
1
vote
1 answer

Removing noexcept from a C++ function, how to deal with the noexcept functions that call it?

Suppose I have a foo function marked as noexcept somewhere in my code. Many other functions call foo, and many of them are also marked as noexcept. Now suppose I have to adjust foo, and now it can throw an exception, so it's not noexcept…
rodrigocfd
  • 6,450
  • 6
  • 34
  • 68
1
vote
1 answer

How to properly specify noexcept with a C++ forwarding reference?

Take, for example, the following: #include #include #include struct Bar { Bar() = default; Bar(Bar const&) noexcept(false) = default; Bar(Bar&&) noexcept(true) = default; }; struct Baz { Baz() =…
jinscoe123
  • 1,467
  • 14
  • 24
1
vote
0 answers

What level of optimization in Clang and GCC removes try-catch around noexcept code?

Consider the following code: struct T { void f() noexcept { /* an arbitrary long piece of code */ } }; void some_function() { T t{}; try { t.f(); } catch(...) { std::cout << "Something is Wrong!!"; // An unreachable line of code …
Koosha
  • 1,492
  • 7
  • 19
1
vote
2 answers

noexcept operator compile-time check

In bellow code I'm trying to use conditional exception specification for functions but compile fails, although if used outside function it works just fine. void may_throw(); // ERROR: expression must have bool type or be convertible to bool void…
metablaster
  • 1,958
  • 12
  • 26
1
vote
1 answer

Compilation error in list while using C++11

I get the following error while compiling my C++ code using std=c++11 option. In file included from /usr/include/c++/7/list:63:0, from /usr/include/qt4/QtCore/qlist.h:51, from…
Soo
  • 885
  • 7
  • 26
1
vote
2 answers

Was there any version of C++ (even pre-standard) were `throw()` did not mean "cannot throw, ever"?

The questions is about history of C++: ISO standards, revised standards (with DR), even draft standards; all count as "a C++". Is there any C++ where this property does not hold: A function declared with an empty throw-specification throw() cannot…
curiousguy
  • 8,038
  • 2
  • 40
  • 58
1
vote
1 answer

How does noexcept in lambda work in vs2012?

While trying to do the compile example from: https://learn.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=vs-2019#exception-specification using Visual studio 2012 []() noexcept { throw 5; }(); I have got the following error: expected…
Kiran Thilak
  • 443
  • 5
  • 15
1
vote
2 answers

Why exception specifiers on overloaded operators '<<' doesn't work to any std::ostream object, but does on those defined in the library?

exemplo.cpp: #include using std::is_same; #include using std::declval; #include using std::ostream; using std::cout; struct Foo final { int value; inline constexpr Foo(const int i) noexcept : value{i}…
1
vote
1 answer

Using `noexcept` on a function that returns a unordered_map

Is it safe to declare a function as noexcept if it initialises and returns a std::unordered_map. As an example: std::unordered_map get_raw_num_map() noexcept { return std::unordered_map( {{1,…
manderc3
  • 79
  • 3
1
vote
1 answer

Find all move constructors and move assignment operators (particularly those without 'noexcept')

In Visual Studio 2017, I'm attempting to add 'noexcept' to all relevant move constructors and move assignment operators so that they can be called by Standard Library containers. Is there a way to find all move constructors and move assignment…
1
vote
1 answer

noexcept requirement of vector not upheld

Technically, noexcept specified move c'tor is requirement of vector to use move instead of copy c'tor. I found that this is not the case with GCC 7. std::vector v; v.push_back(A("555")); //triggers move c'tor The above works as long as A…
code
  • 1,056
  • 10
  • 22
1
vote
2 answers

Why does this noexcept matters for performance so much while a similar other noexcept does not?

I have a piece of generic code whose performance is important to me, because I was challenged to match its running time of a well-known hand-crafted code written in C. Before I began playing with noexcept, my code ran in 4.8 seconds. By putting…
AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
1
vote
1 answer

Functions turning noexcept in C++17?

According to this answer, exception specifications will become part of the function type in C++17. Does this mean that exception specifications for many functions will also change, e.g. functions in the C compatibility headers getting noexcept…
jotik
  • 17,044
  • 13
  • 58
  • 123
1
vote
1 answer

Placement new plus destructor and simple value initialization noexcept semantic

Can one assume in unevaluated context, that (::new (std::declval< void * >()) T())->~T() is semantically (in sense of noexcept, but not in sense of type of expression) equivalent to simple T()? Assume that neither global, nor class-scope operator…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169