Why std::map find() is not declared as noexcept?
Asked Answered
H

1

12

C++14 standard defines the find() member functions of std::map as follows:

iterator find(const key_type& x);
const_iterator find(const key_type& x) const;

Why are these functions not defined as noexcept? What could possibly go wrong inside, that would require to throw an exception or produce undefined behavior (other than not finding an element in which case the function returns an end iterator and no exception throwing would be required anyway)?

Haileyhailfellowwellmet answered 6/1, 2016 at 16:52 Comment(4)
related: #20517759Freida
I don't think the Compare function of a map have to be noexept, so I don't think find() could be noexept since it compare keysCowell
@Cowell Of course, Compare can throw indeed: 23.2.4.1.1 of C++14 states erase(k) does not throw an exception unless that exception is thrown by the container’s Compare object (if any). Please make your comment an answer.Haileyhailfellowwellmet
Well, noexcept(Compare) is still an option.Miticide
C
14

find() are based on the Compare() method of the map, that could throw an exception (imagine the case of a complex key that could be incorrect). So, we can not be sure that find() won't raise an exception.

Cowell answered 6/1, 2016 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.