C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sun, 25 Jun 2023 09:49:07 -0700
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.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-06-25 16:49:10