C++ Logo

std-proposals

Advanced search

Re: Function proposal: generic value_or

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Mon, 5 Jul 2021 18:05:09 -0400
On Mon, Jul 5, 2021 at 5:53 PM Marcin Jaczewski via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> pon., 5 lip 2021 o 23:28 Arthur O'Dwyer via Std-Proposals
> <std-proposals_at_[hidden]> napisał(a):
> >
> > On Mon, Jul 5, 2021 at 4:34 PM Roberto Romani via Std-Proposals <std-proposals_at_[hidden]> wrote:
> >>
> >> If the word “value” in the name of the function may create confusion then the function may be called “coalesce”.
> >>
> >>
> >>
> >> The goal is to have a function that makes cleaner and less error prone things like v = p1 ? *p1 : p2 ? *p2 : defaultValue;
> >
> >
> > So basically, you claim that
> > v = p1 ? *p1 : p2 ? *p2 : defaultValue;
> > is too confusing for the reader to understand at a glance, and you think the reader would prefer to see
>
> Where are clams of "confusing" in that quote (aside of name `value`)?
> Roberto said that it makes code cleaner and this is true as you do not
> repeat the same values.
> and even could reduce complexity further:
> ```
> auto p1 = foo();
> auto p2 = bar();
> return p1 ? *p1 : p2 ? *p1 : defualtValue;
> ```
> vs
> ```
> return std::coalesce(foo(), bar(), defaultValue); //I would prefer
> default on last place but it could be hard to define
> ```
>
> > v = std::coalesce(defaultValue, p1, p2);
> > .
> > And you also claim that it is not useful to provide any special library support for
> > v = p1 ? p1[42] : defaultValue;
> > nor for
> > v = p1 ? *p1 : computeDefaultValue();
> > .
>
> Yes this is true, `p1[42]` could be too specific but
> `computeDefaultValue` be probably useful
> I could push even further to allow to every argument will be callable,

OK, so how do you tell the difference between a pointer-like type that
just so happens to be invokable and an invokable type that just so
happens to be pointer-like? Or even worse, what if the default value
just so happens to be callable?

I don't know if this feature is worth the added complexity. Especially
since it's not exactly easy to use with member functions or cases
where you need arguments.

Received on 2021-07-05 17:05:24