C++ Logo

std-proposals

Advanced search

Re: Yet another member function for std::map

From: Matthew Woehlke <mwoehlke.floss_at_[hidden]>
Date: Mon, 2 Aug 2021 11:29:32 -0400
On 28/07/2021 14.40, Kyle Knoepfel via Std-Proposals wrote:
> if (auto age = ages.value_for(name)) {
>
> Do others find this idea attractive,

Well, I would argue "yes", though Jason may have a point.

(BTW, I also have a free-function implementation of this: see
https://github.com/Kitware/qtextensions/blob/master/core/qtGet.h. Note
that, despite the repo name, this *does* also work with STL.)

> modulo bike-shedding issues like the function name?

I would nominate just `value`. However, I think `value_or`, taking an
"optional" (defaults to `{}`) value to be returned if the requested key
is not found, may be even more beneficial. (I also don't think we should
bikeshed that name, seeing as it's already used by `optional`.)

Qt has good experience demonstrating the usefulness of the latter.

-- 
Matthew

Received on 2021-08-02 10:29:35