diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2024-05-17 10:55:32 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2024-05-22 09:43:41 +0100 |
commit | 1a5e4dd83788ea4c049d354d83ad58a6a3d747e6 (patch) | |
tree | 7d712d545634b2ae05424c82a5da9a7ec5a7a5ba /libstdc++-v3/testsuite/30_threads | |
parent | b33f44ca6ce2dae7502ce138600e1631ffc9232c (diff) | |
download | gcc-1a5e4dd83788ea4c049d354d83ad58a6a3d747e6.zip gcc-1a5e4dd83788ea4c049d354d83ad58a6a3d747e6.tar.gz gcc-1a5e4dd83788ea4c049d354d83ad58a6a3d747e6.tar.bz2 |
libstdc++: Implement std::formatter<std::thread::id> without <sstream> [PR115099]
The std::thread::id formatter uses std::basic_ostringstream without
including <sstream>, which went unnoticed because the test for it uses
a stringstream to check the output is correct.
The fix implemented here is to stop using basic_ostringstream for
formatting thread::id and just use std::format instead.
As a drive-by fix, the formatter specialization is constrained to
require that the thread::id::native_handle_type can be formatted, to
avoid making the formatter ill-formed if the pthread_t type is not a
pointer or integer. Since non-void pointers can't be formatted, ensure
that we convert pointers to const void* for formatting. Make a similar
change to the existing operator<< overload so that in the unlikely case
that pthread_t is a typedef for char* we don't treat it as a
null-terminated string when inserting into a stream.
libstdc++-v3/ChangeLog:
PR libstdc++/115099
* include/bits/std_thread.h: Declare formatter as friend of
thread::id.
* include/std/thread (operator<<): Convert non-void pointers to
void pointers for output.
(formatter): Add constraint that thread::native_handle_type is a
pointer or integer.
(formatter::format): Reimplement without basic_ostringstream.
* testsuite/30_threads/thread/id/output.cc: Check output
compiles before <sstream> has been included.
Diffstat (limited to 'libstdc++-v3/testsuite/30_threads')
-rw-r--r-- | libstdc++-v3/testsuite/30_threads/thread/id/output.cc | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/libstdc++-v3/testsuite/30_threads/thread/id/output.cc b/libstdc++-v3/testsuite/30_threads/thread/id/output.cc index 3c16720..94a6ff0 100644 --- a/libstdc++-v3/testsuite/30_threads/thread/id/output.cc +++ b/libstdc++-v3/testsuite/30_threads/thread/id/output.cc @@ -3,8 +3,27 @@ // { dg-require-gthreads "" } #include <thread> -#include <sstream> + +#include <ostream> #include <format> + +void +test_no_includes(std::ostream& out) +{ + std::thread::id i{}; + // Check stream insertion works without including <sstream>: + out << i; +#if __cpp_lib_formatters >= 202302 + // PR libstdc++/115099 - compilation error: format thread::id + // Verify we can use std::thread::id with std::format without <sstream>: + (void) std::format("{}", i); +#ifdef _GLIBCXX_USE_WCHAR_T + (void) std::format(L"{}", i); +#endif +#endif +} + +#include <sstream> #include <testsuite_hooks.h> void |