C++ Logo

std-proposals

Advanced search

Re: Yet another member function for std::map

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Wed, 28 Jul 2021 18:50:07 -0400
On Wed, Jul 28, 2021 at 2:42 PM Kyle Knoepfel via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Hi all,
>
> C++20 added the std::set::contains and std::map::contains member functions, which perform the equivalent of a find, whose return value is then compared with the appropriate end iterator, resulting in Boolean true or false. We therefore have the nice cleanup:
>
> Pre-C++20: if (set.find(key) != cend(map)) { /* key is present */ }
> Post-C++20: if (set.contains(key)) { /* key is present */ }
>
> For std::map, however, there seems to be less utility for 'contains' as the key's corresponding value is often required if the key is present in the map. For example, this is a common usage pattern:
>
> std::map<std::string, unsigned> ages = …;
> if (auto it = ages.find(name); it != cend(ages)) {
> std::cout << "Age of " << name << ": " << it->second << '\n';
> } else {
> std::cout << name << " not known.\n";
> }
>
> Note all the boilerplate to get to the possibly present value (it->second). In such a case above, 'contains' does not help:
>
> std::map<std::string, unsigned> ages = …;
> if (ages.contains(key)) {
> std::cout
> << "Age of " << name << ": "
> << ages.at(key) // Unnecessarily expensive for value guaranteed to be present
> << '\n';
> } else {
> std::cout << key << " not known.\n";
> }
>
> This situation can be made simpler using an interface with std::optional-like semantics:
>
> std::map<std::string, unsigned> ages = …;
> if (auto age = ages.value_for(name)) {
> std::cout << "Age of " << name << ": " << *age << '\n';
> } else {
> std::cout << name << " not known.\n";
> }
>
> where the type of age is a handle-like class that refers to the value of the 'name' entry (not quite like std::optional as we don't want to copy the actual value).
>
> Do others find this idea attractive, modulo bike-shedding issues like the function name?

No.

The difference in lines here is:

Current:
if(auto it = ages.find(name); it != ages.end())

Proposed:
if(auto age = ages.value_for(name)))

So, you're talking about a difference of twelve characters. Not
exactly what I would call "boilerplate".

Also, it normalizes practice that is somewhat dubious. Indeed, we
added special variable declaration syntax to C++17 specifically to
*discourage* this kind of thing.

Received on 2021-07-28 17:50:21