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
2
votes
3 answers

Proper Usage of `noexcept`

I'm trying to figure out the proper way of using the noexcept attribute. I've already spent half a day trying to understand it, but I'm not sure I did. And I don't want to use or not to use noexcept everywhere mindlessly. Question: Do I understand…
ENIAC
  • 813
  • 1
  • 8
  • 19
2
votes
1 answer

Why do std::flat_set and std::flat_map have some noexcept functions while other container adapters don't?

I noticed that std::flat_set and std::flat_map container adaptors provide some noexcept member functions, in particular there are the followings: [[nodiscard]] bool empty() const noexcept; size_type size() const noexcept; However, all the other…
LoS
  • 448
  • 1
  • 3
  • 15
2
votes
1 answer

Does specifying a constructor as noexcept implicitly result in the nothrow version of the new operator being used in C++?

If I specify a constructor as noexcept, does the nothrow version of the new operator implicitly get used when the object is dynamically instantiated? For example: class Something { public: Something() noexcept; }; ... Something *s = new…
Billy
  • 5,179
  • 2
  • 27
  • 53
2
votes
1 answer

Why not conditional noexcept?

The standard library doesn't typically use conditional noexcept. It is described in this paper: Each library function having a wide contract, that the LWG agree cannot throw, should be marked as unconditionally noexcept. If a library swap…
2
votes
1 answer

Is std::span constructor missing noexcept?

According to cppreference constructor (2) of std::span is defined as template< class It > explicit(extent != std::dynamic_extent) constexpr span( It first, size_type count ); with its exceptions listed as 2) Throws nothing. If this constructor…
Chris_F
  • 4,991
  • 5
  • 33
  • 63
2
votes
0 answers

apple clang 12 vector noexcept check fails

The question is very simple. I am using Apple Clang 12 and this fails: static_assert(std::is_nothrow_move_assignable_v>); I cannot figure out why even by navigating their macros in the source code to determine whether noexcept is…
Germán Diago
  • 7,473
  • 1
  • 36
  • 59
2
votes
2 answers

Type traits `is_noexcept`, `add_noexcept`, and `remove_noexcept`?

Motivation: In the implementation of P0288 std::move_only_function, I'd like to write a non-allocating special case for conversion from move_only_function to move_only_function: move_only_function f = []()…
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
2
votes
0 answers

Why vector.tcc implement _M_fill_insert by __uninitialized_move_a?

In gcc vector.tcc::_M_fill_insert function (line524), the implementation uses case 1: __uninitialized_move_a(line562) for case of uninitialized but existing memory; case 2: __uninitialized_move_if_noexcept_a(line580) for newly allocated memory. I…
2
votes
1 answer

Can getters be marked `noexcept`?

For a class such as this: class CharMatrix { public: . . . private: int m_Y_AxisLen; int m_X_AxisLen; char m_fillCharacter; mutable std::vector< std::vector > m_characterMatrix; } inline const int& CharMatrix::getY_AxisLen(…
digito_evo
  • 3,216
  • 2
  • 14
  • 42
2
votes
1 answer

Should Lippincott functions be declared noexcept?

EDIT: Another way to ask this question, in perspective, is to ask: Should Lippincott functions "catch all"? Should Lippincott functions be declared noexcept?, does it matter? After all, a version of this function in which all exceptions are…
alfC
  • 14,261
  • 4
  • 67
  • 118
2
votes
0 answers

Why GCC implementation of std::vector operator[] has noexcept specifier

Here - https://en.cppreference.com/w/cpp/container/vector/operator_at it says that operator[] has no noexcept specifier. But If I write: cout << noexcept(declval>()[declval()]) << endl; I get: true Compiled with g++.exe…
Aisec Nory
  • 385
  • 1
  • 8
2
votes
0 answers

why is std::midpoint not noexcept for pointers?

c++20 introduces std::midpoint: template< class T > constexpr T midpoint( T a, T b ) noexcept; template< class T > constexpr T* midpoint( T* a, T* b ); Both gcc and clang make it noexcept though.
VainMan
  • 2,025
  • 8
  • 23
2
votes
1 answer

Reference to initializer_list in noexcept specifier of std::optional

I have a question about this code: explicit constexpr optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args) noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>) :…
2
votes
1 answer

how to determine if split_apply() is noexcept?

I have this forwarding function, that may or may not throw, depending on what the arguments are: template constexpr void split_apply(auto f, auto&& ...a) // noexcept(?) { // transforms provided tuple t into a tuple of N-element…
user1095108
  • 14,119
  • 9
  • 58
  • 116
2
votes
0 answers

static assertion on noexcept for protected constructors

I'm attempting to assert following protected class constructors may or may not throw as follows: #include #include class X { protected: X() noexcept(false) { } X(int) noexcept { } }; int main() { // should fail …
metablaster
  • 1,958
  • 12
  • 26