C++ Logo


Advanced search

Re: Initialisers in ternary operators

From: Jorg Brown <jorg.brown_at_[hidden]>
Date: Sat, 12 Sep 2020 18:34:56 -0700
If you were going to go down the route of findOr(key, value), then I'd
suggest a routine,

std::optional<Value &>
std::find_option(Container &&c, Key &&key);

And then
  findOr(container, key, value)
would be expressed as
  find_option(container, key).value_or(value)

And yes, there are issues with details here, such as std::optional not
allowing reference types. Still, a find that doesn't make us compare
against .end would be immensely useful in many contexts.

For example, this code:
  if (*auto It = Cont.find(42);* It == Cont.end()) return 0; else return
would become simpler:
  if (*auto It = find_option(Cont, 42)*) return *It; else return 0;

Also, while I'd support this, it should probably go into another mail
thread, if anyone really wants to pursue it.

On Fri, Sep 11, 2020 at 12:19 AM Dmitry Dmitry via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Just write and use a find wrapper like these:
> It was just an example.
> If we start considering solving it via wrappers/helpers, then I would just
> create something like findOr(key, value) function.
> But, anyway, it seems that the idea is not worth it...
> Thanks for the feedback.
> --
> Dmitry
> *Sent from gmail*
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2020-09-12 20:35:13