[PATCH] libstdc++: Constrain operator<< for chrono::local_type
Tomasz Kaminski
tkaminsk@redhat.com
Tue Sep 23 12:35:13 GMT 2025
This is done in
https://gcc.gnu.org/pipermail/libstdc++/2025-September/063601.html.
(Patch 1/2 is another small cleanup).
On Mon, Sep 22, 2025 at 9:47 AM Tomasz Kaminski <tkaminsk@redhat.com> wrote:
> The formatter for chrono::local_time needs to also be updated, to reflect
> that operator<< is not
> always available, thus "{}" format spec should not be supported.
> Essentially a same approach of for sys_time specialization needs to be
> taken:
> * __defSec should have empty _M_chrono_spec if the local time is not
> stream instertable
> * parse function should throw an if _M_chrono_spec is empty, this check
> an be be omitted for __stream_insertable
> types.
>
>
>
> On Fri, Sep 19, 2025 at 3:23 PM Jonathan Wakely <jwakely@redhat.com>
> wrote:
>
>> This was reported as LWG 4257 and moved to Tentatively Ready status.
>>
>> libstdc++-v3/ChangeLog:
>>
>> * include/bits/chrono_io.h: Remove unused <iomanip> header.
>> (operator<<): Constrain overload for chrono::local_time.
>> * testsuite/std/time/clock/local/io.cc: Check constraints.
>> ---
>>
>> Tested powerpc64le-linux.
>>
>> libstdc++-v3/include/bits/chrono_io.h | 4 +++-
>> libstdc++-v3/testsuite/std/time/clock/local/io.cc | 8 ++++++++
>> 2 files changed, 11 insertions(+), 1 deletion(-)
>>
>> diff --git a/libstdc++-v3/include/bits/chrono_io.h
>> b/libstdc++-v3/include/bits/chrono_io.h
>> index 809d795cbf2b..593a927811a6 100644
>> --- a/libstdc++-v3/include/bits/chrono_io.h
>> +++ b/libstdc++-v3/include/bits/chrono_io.h
>> @@ -37,7 +37,6 @@
>> #if __cplusplus >= 202002L
>>
>> #include <sstream> // ostringstream
>> -#include <iomanip> // setw, setfill
>> #include <format>
>> #include <charconv> // from_chars
>> #include <stdexcept> // __sso_string
>> @@ -4014,6 +4013,9 @@ namespace __detail
>> inline basic_ostream<_CharT, _Traits>&
>> operator<<(basic_ostream<_CharT, _Traits>& __os,
>> const local_time<_Duration>& __lt)
>> + // _GLIBCXX_RESOLVE_LIB_DEFECTS
>> + // 4257. Stream insertion for chrono::local_time should be
>> constrained
>> + requires requires(const sys_time<_Duration>& __st) { __os << __st; }
>> {
>> __os << sys_time<_Duration>{__lt.time_since_epoch()};
>> return __os;
>> diff --git a/libstdc++-v3/testsuite/std/time/clock/local/io.cc
>> b/libstdc++-v3/testsuite/std/time/clock/local/io.cc
>> index 67818e876497..6efd81a87c2a 100644
>> --- a/libstdc++-v3/testsuite/std/time/clock/local/io.cc
>> +++ b/libstdc++-v3/testsuite/std/time/clock/local/io.cc
>> @@ -125,6 +125,14 @@ test_parse()
>> VERIFY( tp.time_since_epoch() == 0s );
>> }
>>
>> +// LWG 4257. Stream insertion for chrono::local_time should be
>> constrained
>> +template<typename T>
>> +concept ostream_insertable = requires (std::ostream& o, const T& t) { o
>> << t; };
>> +using D = std::chrono::duration<double>;
>> +static_assert( ostream_insertable<std::chrono::local_days> );
>> +static_assert( ostream_insertable<std::chrono::local_seconds> );
>> +static_assert( ! ostream_insertable<std::chrono::local_time<D>> );
>> +
>> int main()
>> {
>> test_ostream();
>> --
>> 2.51.0
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20250923/4760485e/attachment.htm>
More information about the Libstdc++
mailing list