Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 666
LoS Avatar asked Feb 02 '26 17:02

LoS


1 Answers

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.

like image 166
LoS Avatar answered Feb 05 '26 08:02

LoS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!