c++stlnoexceptc++23

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 container adaptors provide the same member functions as non-noexcept:

[[nodiscard]] bool empty() const;
size_type size() const;

Is there a reason for this difference?


Solution

  • Adding the noexcept specific to a function that is not guaranteed to be non-throwing can be dangerous. This problem occurs precisely with the std::stack, std::queue and std::priority_queue container adapters, which provide the empty() and size() member functions since C++98, and therefore did not force the underlying container's member functions to be non-throwing.