Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #67227 > unrolled thread

Is there a reason that the std::map contains() function lacks a noexcept specifier?

Started byDaniel <danielaparker@gmail.com>
First post2019-07-31 07:10 -0700
Last post2019-08-01 03:04 -0700
Articles 7 — 5 participants

Back to article view | Back to comp.lang.c++


Contents

  Is there a reason that the std::map contains() function lacks a noexcept specifier? Daniel <danielaparker@gmail.com> - 2019-07-31 07:10 -0700
    Re: Is there a reason that the std::map contains() function lacks a noexcept specifier? Bo Persson <bo@bo-persson.se> - 2019-07-31 18:12 +0200
      Re: Is there a reason that the std::map contains() function lacks a noexcept specifier? Daniel <danielaparker@gmail.com> - 2019-07-31 20:29 -0700
    Re: Is there a reason that the std::map contains() function lacks a noexcept specifier? Öö Tiib <ootiib@hot.ee> - 2019-07-31 14:55 -0700
      Re: Is there a reason that the std::map contains() function lacks a noexcept specifier? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-08-01 05:44 +0000
        Re: Is there a reason that the std::map contains() function lacks a noexcept specifier? Fraser Ross <fraser.ross8ATbtinternet.com@com> - 2019-08-01 09:18 +0100
          Re: Is there a reason that the std::map contains() function lacks a noexcept specifier? Öö Tiib <ootiib@hot.ee> - 2019-08-01 03:04 -0700

#67227 — Is there a reason that the std::map contains() function lacks a noexcept specifier?

FromDaniel <danielaparker@gmail.com>
Date2019-07-31 07:10 -0700
SubjectIs there a reason that the std::map contains() function lacks a noexcept specifier?
Message-ID<4563d007-9952-4efc-ad36-315d868724bc@googlegroups.com>
The cppreference documentation doesn't identify any exceptions.

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

Thanks,
Daniel

[toc] | [next] | [standalone]


#67228

FromBo Persson <bo@bo-persson.se>
Date2019-07-31 18:12 +0200
Message-ID<gqdsvpF2dnoU1@mid.individual.net>
In reply to#67227
On 2019-07-31 at 16:10, Daniel wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#67236

FromDaniel <danielaparker@gmail.com>
Date2019-07-31 20:29 -0700
Message-ID<8737e9ed-c7c0-40ec-abc6-1d296c2c5526@googlegroups.com>
In reply to#67228
On Wednesday, July 31, 2019 at 12:12:54 PM UTC-4, Bo Persson wrote:
> On 2019-07-31 at 16:10, Daniel wrote:
> > 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

[toc] | [prev] | [next] | [standalone]


#67231

FromÖö Tiib <ootiib@hot.ee>
Date2019-07-31 14:55 -0700
Message-ID<83084ca5-025f-44c0-8819-94f59e4b7c0d@googlegroups.com>
In reply to#67227
On Wednesday, 31 July 2019 17:10:24 UTC+3, Daniel  wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#67237

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2019-08-01 05:44 +0000
Message-ID<slrnqk4v1i.cdo.grahn+nntp@frailea.sa.invalid>
In reply to#67231
On Wed, 2019-07-31, Öö Tiib wrote:
...
> 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   .

[toc] | [prev] | [next] | [standalone]


#67238

FromFraser Ross <fraser.ross8ATbtinternet.com@com>
Date2019-08-01 09:18 +0100
Message-ID<hfx0F.49174$N04.2101@usenetxs.com>
In reply to#67237
On 01/08/2019 06:44, Jorgen Grahn wrote:
> On Wed, 2019-07-31, Öö Tiib wrote:
> ...
>> 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
> 

contains can stop after finding an equivalent element in a multimap or 
multiset.

Fraser.

[toc] | [prev] | [next] | [standalone]


#67239

FromÖö Tiib <ootiib@hot.ee>
Date2019-08-01 03:04 -0700
Message-ID<1a27adf5-0c34-462d-891f-e70ee9192328@googlegroups.com>
In reply to#67238
On Thursday, 1 August 2019 11:18:33 UTC+3, Fraser Ross  wrote:
> On 01/08/2019 06:44, Jorgen Grahn wrote:
> > On Wed, 2019-07-31, Öö Tiib wrote:
> > ...
> >> 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);

Note that in actual code it can have non-confusing form like:

    if (employees.count(someone)!=0) 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.

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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web