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)?

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
PowerGamer
  • 2,106
  • 2
  • 17
  • 33
  • 4
    related: http://stackoverflow.com/questions/20517259/why-vector-access-operators-are-not-specified-as-noexcept – NathanOliver Jan 06 '16 at 17:01
  • 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 keys – 88877 Jan 06 '16 at 17:07
  • 1
    @88877 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. – PowerGamer Jan 06 '16 at 17:23
  • 6
    Well, `noexcept(Compare)` is still an option. – Karoly Horvath Jan 06 '16 at 17:26

1 Answers1

13

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.

88877
  • 485
  • 3
  • 11