C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Making the converting constructor of std::optional less greedy

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Thu, 7 Dec 2023 19:01:49 +0200
On Thu, 7 Dec 2023 at 17:29, Edward Catmur <ecatmur_at_[hidden]> wrote:
>> It was an intentional design decision to give the T in optional<T> a
>> chance to convert directly
>> from any U, including U=optional<Z>, if it can, instead of wrapping
>> into an optional<Z> first
>> and then converting T from Z.
>>
>> You may think that makes optional not play nice with templated
>> conversion operators, but
>> if we make the change you suggest, then someone will come in and say
>> that optional doesn't
>> play nice with templated conversion constructors.
>
>
> We're not talking about U -> optional<Z> -> optional<T> though

Nobody said we are. optional<Z> is the U incoming to the optional(U&&)
constructor. That's the constructor chosen if T
constructs from optional, and that's the constructor chosen if T just
converts from U in general and U isn't an optional.
For both of those cases, it's intentional that T gets the chance to
construct from the incoming U, like the Proxy mentioned.
It's intentional that it optional doesn't try to convert the Proxy to
an optional<Whatever> as a preference overy just
converting the Proxy to U.

> we're talking about U -> optional<T> directly. Intuitively it seems that that should be preferred over U -> T -> optional<T>.

optional<T> converts like a T, as far as possible. Because optional<T>
behaves like a T, as far as possible.

> Another case that I think is ultimately related:

Looks quite separate to me.

Received on 2023-12-07 17:02:03