For optional's template<class U = T> optional<T>& operator=(U&& v);
the standard demands that (see [optional.assign]/3.16):
This function shall not participate in overload resolution unless ...
conjunction_v<is_scalar<T>, is_same<T, decay_t<U>>>
isfalse
...
Why do we have to exclude case when assigning a scalar of type U == T
?
The class template std::optional manages an optional contained value, i.e. a value that may or may not be present. A common use case for optional is the return value of a function that may fail.
What's more, std::optional doesn't need to allocate any memory on the free store. std::optional is a part of C++ vocabulary types along with std::any , std::variant and std::string_view .
Abstract. std::optional is an important vocabulary type in C++17. Some uses of it are verbose and would benefit from operations which allow functional composition. I propose adding transform, and_then, and or_else member functions to std::optional to support this monadic style of programming.
C++17 introduced std::optional<T> which lets you augment the values of a type T with a bonus value known as std::nullopt which semantically represents the absence of a value. A std::optional which holds the value std::nullopt is known as empty.
This exists to support:
optional<int> o(42);
o = {}; // <== we want this to reset o
We have a bunch of assignment overloads, which take:
nullopt_t
optional const&
optional&&
U&&
optional<U> const&
optional<U>&&
For scalars, specifically, #4 would be a standard conversion whereas anything else would be a user-defined conversion - so it would be the best match. However, the result of that would be assigning o
to be engaged with a value of 0
. That would mean that o = {}
could potentially mean different things depending on the type of T
. Hence, we exclude scalars.
For non-scalars, #4 and #3 would be equivalent (both user-defined conversions), and #3 would win by being a non-template. No problem there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With