Discussion:
Is there a reason that the std::map contains() function lacks a noexcept specifier?
(too old to reply)
Daniel
2019-07-31 14:10:11 UTC
Permalink
The cppreference documentation doesn't identify any exceptions.

https://en.cppreference.com/w/cpp/container/map/contains

Thanks,
Daniel
Bo Persson
2019-07-31 16:12:40 UTC
Permalink
Post by Daniel
The cppreference documentation doesn't identify any exceptions.
https://en.cppreference.com/w/cpp/container/map/contains
Thanks,
Daniel
The function itself is perhaps unlikely to throw any exceptions, but any
comparisons would use the Compare template parameter which is not
limited to noexcept operations.


Bo Persson
Daniel
2019-08-01 03:29:16 UTC
Permalink
Post by Bo Persson
Post by Daniel
The cppreference documentation doesn't identify any exceptions.
The function itself is perhaps unlikely to throw any exceptions, but any
comparisons would use the Compare template parameter which is not
limited to noexcept operations.
Thanks,
Daniel
Öö Tiib
2019-07-31 21:55:28 UTC
Permalink
Post by Daniel
The cppreference documentation doesn't identify any exceptions.
https://en.cppreference.com/w/cpp/container/map/contains
Hmm? Be constructive. How you suggest to define it then?
Let me try ...

bool contains( const Key& key ) const
noexcept(noexcept(key_comp()(key, key)));

That? Does that work?

Note that contains() is C++2020 map feature and C++2020 standard does
not exist yet. I don't even understand why to add it since it does
exactly same thing that std::map::count() already does. However
if it is possible to add such noexcept specifications then these
should perhaps be added to *lot* of other functions as well.
Jorgen Grahn
2019-08-01 05:44:18 UTC
Permalink
On Wed, 2019-07-31, Öö Tiib wrote:
...
Post by Öö Tiib
Note that contains() is C++2020 map feature and C++2020 standard does
not exist yet. I don't even understand why to add it since it does
exactly same thing that std::map::count() already does.
Probably to help us write more readable code:

if (employees.count(someone)) fire(someone);
if (employees.contains(someone)) fire(someone);

The latter reads better IMO. On the other hand, I think in 90% of
cases you want to find() the element so you can access it if it
exists.

/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Fraser Ross
2019-08-01 08:18:19 UTC
Permalink
Post by Jorgen Grahn
...
Post by Öö Tiib
Note that contains() is C++2020 map feature and C++2020 standard does
not exist yet. I don't even understand why to add it since it does
exactly same thing that std::map::count() already does.
if (employees.count(someone)) fire(someone);
if (employees.contains(someone)) fire(someone);
The latter reads better IMO. On the other hand, I think in 90% of
cases you want to find() the element so you can access it if it
exists.
/Jorgen
contains can stop after finding an equivalent element in a multimap or
multiset.

Fraser.
Öö Tiib
2019-08-01 10:04:43 UTC
Permalink
Post by Fraser Ross
Post by Jorgen Grahn
...
Post by Öö Tiib
Note that contains() is C++2020 map feature and C++2020 standard does
not exist yet. I don't even understand why to add it since it does
exactly same thing that std::map::count() already does.
if (employees.count(someone)) fire(someone);
Note that in actual code it can have non-confusing form like:

if (employees.count(someone)!=0) fire(someone);
Post by Fraser Ross
Post by Jorgen Grahn
if (employees.contains(someone)) fire(someone);
The latter reads better IMO. On the other hand, I think in 90% of
cases you want to find() the element so you can access it if it
exists.
/Jorgen
contains can stop after finding an equivalent element in a multimap or
multiset.
Good catch!

Even there it can have same issue that empty() versus size()!=0 had.
The empty() is shorter and was cheaper for some containers but
programmers kept massively using size()!=0 (and its equivalents) so
committee had to require size() to be O(1) for all containers.
It is hard to figure why human brain works like that but programming
language has to support it to write efficient programs.

Loading...