18

I noticed that the std::vector container's swap function has a different noexcept-specification than all other containers. Specifically, the function is noexcept if the expression std::allocator_traits<Allocator>::propagate_on_container_swap || std::allocator_traits<Allocator>::is_always_equal is true, but other containers require only the expression std::allocator_traits<Allocator>::is_always_equal to be true.

Since the behavior of the swap functions is the same, why is the noexcept-specification different in just the std::vector container?

LoS
  • 448
  • 1
  • 3
  • 15
  • 1
    Howard Hinnant apparently answered your question in [this comment](https://stackoverflow.com/questions/28309516/allocator-aware-container-and-propagate-on-container-swap#comment44973265_28310100)! – sigma May 13 '23 at 23:11
  • 2
    The comment explains why the swap function's noexcept-specification was implemented that way, but it does not answer the question "why is the noexcept-specification different just in the `std::vector` container". – LoS May 14 '23 at 09:06
  • 1
    Unfortunately I could only find links to non-public discussion on this topic. Maybe it was just caution when they introduced the conditional `noexcept` specifiers for both swap and move operations. It is much easier to verify that it won't cause implementation problems for vector than for the other containers and vector should also be higher priority. The standard library implementation is allowed to use stricter `noexcept` specification than specified in the standard, so that's just the minimum that all implementations should agree upon. – user17732522 May 14 '23 at 11:03
  • @user17732522, you may be right. However, if the effects and the behavior of the swap function is the same in all containers, I think the noexcept-specification should be the same to be consistent and guarantee a stricter minimum specification in all implementations. Does it have sense? – LoS May 14 '23 at 11:27
  • 2
    The history of this topic is certainly interesting. The `noexcept(false)` situation can only arise here when using custom, stateful allocators, since the `std::allocator` template is stateless and therefore always equal. Writing such an allocator is something most C++ programmers will not need to do, but apparently Bloomberg had a major codebase using these. Consequently, Bloomberg developers have provided most of the input I found on allocators, swapping and noexcept. The "Lakos rule" on the use of noexcept in the standard library is named after one of them. – sigma May 14 '23 at 20:13
  • 2
    After reading this, it would not surprise me if they were simply using those allocators primarily or exclusively with vectors, and therefore were most interested in making this particular change to the standard. – sigma May 14 '23 at 20:35

0 Answers0