C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Default value_or

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Wed, 4 Dec 2024 15:22:57 +0000
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

Received on 2024-12-04 15:24:13