|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2024-12-21 13:32:52
Andrey Semashev wrote:
> On 12/21/24 15:11, Peter Dimov via Boost wrote:
> > Christian Mazakas wrote:
> > What this user has always wanted is to not have to write this:
> >
> > auto it = map.find( key );
> > if( it != map.end() )
> > {
> > // do something with it->second
> > }
> >
> > That's why I'm always defining a helper function `lookup` that returns
> > a pointer, which allows me to write this instead:
> >
> > if( auto p = lookup( map, key ) )
> > {
> > // do something with *p
> > }
>
> I'll note that with C++17 you can also write this:
>
> if ( auto it = map.find( key ); it != map.end() )
> {
> // do something with it->second
> }
>
> But each of the three variants seem pretty much equivalent to me, from the
> code clarity standpoint.
It was admittedly much worse in C++03, where you had to spell out
std::map<key_type, mapped_type>::const_iterator it
in its entirety. Plus a typename in front if you happened to be in a template.
C++17 makes it almost palatable, except for the need to repeat `it->second`.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk