C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Add operator>> overloads to std::optional for streaming input

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Sun, 25 Jun 2023 20:26:54 +0300
On 6/25/23 19:49, Thiago Macieira via Std-Proposals wrote:
> On Sunday, 25 June 2023 01:44:42 PDT Andrey Semashev via Std-Proposals wrote:
>> Such a sentinel would need to have a distinct string representation from
>> any other value of the type T in optional<T>. With arbitrary T and no
>> explicit markup for the formatted value of T in the string, I don't
>> think you will be able to define such a sentinel.
>>
>> For example, how would you define it for std::optional<std::string>?
>
> You discard compatibility between std::optional<T> and direct T. That is,
> require that the streaming be of the same type both in and out, which I think
> is a reasonable requirement.
>
> Then, the simplest solution is to stream a boolean before, indicating whether
> the contents are engaged or not.

Yes, that's what Boost.Optional does.

Received on 2023-06-25 17:27:10