C++ Logo

std-proposals

Advanced search

[std-proposals] Default value_or

From: Baruch Burstein <bmburstein_at_[hidden]>
Date: Wed, 4 Dec 2024 17:16:01 +0200
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?

Received on 2024-12-04 15:16:38