Date: Wed, 28 Jul 2021 16:50:50 -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 you also want to be able to change the value being referenced?
if (auto age = ages.value_for(name)) {
age++; // it's a new year!
} else {
std::cout << name << "not known\n:";
}
> Do others find this idea attractive, modulo bike-shedding issues like the
> function name?
>
> Thanks,
> Kyle
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/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 you also want to be able to change the value being referenced?
if (auto age = ages.value_for(name)) {
age++; // it's a new year!
} else {
std::cout << name << "not known\n:";
}
> Do others find this idea attractive, modulo bike-shedding issues like the
> function name?
>
> Thanks,
> Kyle
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
-- Be seeing you, Tony
Received on 2021-07-28 15:51:04