C++ Logo


Advanced search

Re: Function proposal: generic value_or

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Tue, 6 Jul 2021 00:53:13 +0300
On Tue, 6 Jul 2021 at 00:28, Arthur O'Dwyer via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> 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
> v = std::coalesce(defaultValue, p1, p2);

Absolutely. The readability difference is astoundingly large.
Real-world identifiers aren't that short, either,
so the nested ternary will split over multiple lines, which makes it
very annoying to read at a glance,
whereas a function call is very easy to read.

> 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();

Well, one possibility is to steal the value_or_else from
In this particular case it would be a way to invoke a function if our
nullablehandle is not null, instead of when it is null.

Received on 2021-07-05 16:53:41