C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Default value_or

From: Charles R Hogg <charles.r.hogg_at_[hidden]>
Date: Thu, 5 Dec 2024 15:35:20 -0500
Meanwhile, you don't need to wait on updates to the standard to make your
code more concise! You can create a type which converts implicitly to any
default-constructible type. Then you can simply pass an instance of that
type to `.value_or(...)`.

See this example: https://godbolt.org/z/eevbG1vrY

On Wed, Dec 4, 2024 at 10:28 AM Baruch Burstein via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Thank you
>
> On Wed, Dec 4, 2024 at 5:24 PM Jonathan Wakely <cxx_at_[hidden]> wrote:
>
>>
>>
>> On Wed, 4 Dec 2024 at 15:16, Baruch Burstein via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>>
>>> Hi,
>>> std::optional and std::expected have a function `T value_or<U>(...)`
>>> which returns the value passed to the function if no value is available in
>>> the object. It gets annoyingly verbose when I want a default-constructed T
>>> to be the default value and T is some long type name. I would like to
>>> suggest having an overload that does not accept any parameter and returns a
>>> default constructed T. Effectively it would be something like this:
>>>
>>> constexpr T value_or() const& { return value_or(T{}); }
>>> constexpr T value_or() && { return value_or(T{}); }
>>>
>>> Currently I need to either alias type names or use the annoying:
>>>
>>> my_optional_obj.value_or(some_long_ns::some_long_type_name{});
>>>
>>> Any opinions?
>>>
>>
>> See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3413r0.pdf
>>
>> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2024-12-05 20:35:36