From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Implement "Printing blank lines with println" for C++23
Date: Wed, 17 Apr 2024 17:40:51 +0100 [thread overview]
Message-ID: <20240417164148.1062828-1-jwakely@redhat.com> (raw)
Tested x86_64-linux and x86_64-freebsd. Pushed to trunk.
-- >8 --
This was recently approved for C++26 at the Tokyo meeting. As suggested
by Stephan T. Lavavej, I'm defining it as an extension for C++23 mode
(when std::print and std::prinln were first added) rather than as a new
C++26 feature. Both MSVC and libc++ have agreed to do this too.
libstdc++-v3/ChangeLog:
* include/std/ostream (println(ostream&)): Define new overload.
* include/std/print (println(FILE*), println()): Likewise.
* testsuite/27_io/basic_ostream/print/2.cc: New test.
* testsuite/27_io/print/1.cc: Remove unused header.
* testsuite/27_io/print/3.cc: New test.
---
libstdc++-v3/include/std/ostream | 12 +++++
libstdc++-v3/include/std/print | 14 +++++
.../testsuite/27_io/basic_ostream/print/2.cc | 47 ++++++++++++++++
libstdc++-v3/testsuite/27_io/print/1.cc | 1 -
libstdc++-v3/testsuite/27_io/print/3.cc | 53 +++++++++++++++++++
5 files changed, 126 insertions(+), 1 deletion(-)
create mode 100644 libstdc++-v3/testsuite/27_io/basic_ostream/print/2.cc
create mode 100644 libstdc++-v3/testsuite/27_io/print/3.cc
diff --git a/libstdc++-v3/include/std/ostream b/libstdc++-v3/include/std/ostream
index a136399ad0b..8a21758d0a3 100644
--- a/libstdc++-v3/include/std/ostream
+++ b/libstdc++-v3/include/std/ostream
@@ -995,6 +995,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
std::print(__os, "{}\n",
std::format(__fmt, std::forward<_Args>(__args)...));
}
+
+ // Defined for C++26, supported as an extension to C++23.
+ inline void println(ostream& __os)
+ {
+#if defined(_WIN32) && !defined(__CYGWIN__)
+ if constexpr (__unicode::__literal_encoding_is_utf8())
+ std::vprint_unicode(__os, "\n", std::make_format_args());
+ else
+#endif
+ __os.put('\n');
+ }
+
#endif // __cpp_lib_print
#endif // C++11
diff --git a/libstdc++-v3/include/std/print b/libstdc++-v3/include/std/print
index d44033469de..0c259d04de3 100644
--- a/libstdc++-v3/include/std/print
+++ b/libstdc++-v3/include/std/print
@@ -136,6 +136,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
vprint_nonunicode(string_view __fmt, format_args __args)
{ std::vprint_nonunicode(stdout, __fmt, __args); }
+ // Defined for C++26, supported as an extension to C++23.
+ inline void println(FILE* __stream)
+ {
+#if defined(_WIN32) && !defined(__CYGWIN__)
+ if constexpr (__unicode::__literal_encoding_is_utf8())
+ std::vprint_unicode(__stream, "\n", std::make_format_args());
+ else
+#endif
+ if (std::putc('\n', __stream) == EOF)
+ __throw_system_error(EIO);
+ }
+
+ inline void println() { std::println(stdout); }
+
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // __cpp_lib_print
diff --git a/libstdc++-v3/testsuite/27_io/basic_ostream/print/2.cc b/libstdc++-v3/testsuite/27_io/basic_ostream/print/2.cc
new file mode 100644
index 00000000000..5d1e3efdbf7
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/basic_ostream/print/2.cc
@@ -0,0 +1,47 @@
+// { dg-additional-options "-lstdc++exp" { target { *-*-mingw* } } }
+// { dg-do run { target c++23 } }
+// { dg-require-fileio "" }
+
+#include <ostream>
+#include <spanstream>
+#include <string_view>
+#include <iostream>
+#include <iomanip>
+#include <testsuite_hooks.h>
+
+void
+test_println_blank_ostream()
+{
+ char buf[4];
+ std::spanstream os(buf);
+ std::println(os);
+ std::string_view txt(os.span());
+ VERIFY( txt == "\n" );
+}
+
+void
+test_errors()
+{
+ // Failure to generate output is reported by setting badbit.
+ std::stringstream in(std::ios::in);
+ std::println(in); // No exception here.
+ VERIFY(in.bad());
+#ifdef __cpp_exceptions
+ in.clear();
+ in.exceptions(std::ios::badbit);
+ try
+ {
+ std::println(in); // Should throw now.
+ VERIFY(false);
+ }
+ catch (const std::ios::failure&)
+ {
+ }
+#endif
+}
+
+int main()
+{
+ test_println_blank_ostream();
+ test_errors();
+}
diff --git a/libstdc++-v3/testsuite/27_io/print/1.cc b/libstdc++-v3/testsuite/27_io/print/1.cc
index d570f7938be..f6585d9880a 100644
--- a/libstdc++-v3/testsuite/27_io/print/1.cc
+++ b/libstdc++-v3/testsuite/27_io/print/1.cc
@@ -4,7 +4,6 @@
#include <print>
#include <cstdio>
-#include <spanstream>
#include <testsuite_hooks.h>
#include <testsuite_fs.h>
diff --git a/libstdc++-v3/testsuite/27_io/print/3.cc b/libstdc++-v3/testsuite/27_io/print/3.cc
new file mode 100644
index 00000000000..ffcf7337ce5
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/print/3.cc
@@ -0,0 +1,53 @@
+// { dg-additional-options "-lstdc++exp" { target { *-*-mingw* } } }
+// { dg-do run { target c++23 } }
+// { dg-require-fileio "" }
+
+#include <print>
+#include <cstdio>
+#include <testsuite_hooks.h>
+#include <testsuite_fs.h>
+
+void
+test_println_blank()
+{
+ std::print("1");
+ std::println();
+ std::println("2");
+ // { dg-output "1\n2" }
+}
+
+void
+test_println_blank_file()
+{
+ __gnu_test::scoped_file f;
+ FILE* strm = std::fopen(f.path.string().c_str(), "w");
+ VERIFY( strm );
+ std::println(strm);
+ std::fclose(strm);
+
+ std::ifstream in(f.path);
+ std::string txt(std::istreambuf_iterator<char>(in), {});
+ VERIFY( txt == "\n" );
+}
+
+void
+test_errors()
+{
+#ifdef __cpp_exceptions
+ try
+ {
+ std::println(stdin);
+ VERIFY(false);
+ }
+ catch (const std::system_error&)
+ {
+ }
+#endif
+}
+
+int main()
+{
+ test_println_blank();
+ test_println_blank_file();
+ test_errors();
+}
--
2.44.0
reply other threads:[~2024-04-17 16:41 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240417164148.1062828-1-jwakely@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).