public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Add operator bool to <charconv> result types (P2497R0)
Date: Fri, 15 Sep 2023 10:24:41 +0100	[thread overview]
Message-ID: <20230915092647.2192116-1-jwakely@redhat.com> (raw)

Tested x86_64-linux. Pushed to trunk.

This was supposed to be in C++23, but I messed up the polls at the last
meeting for C++23 ballot resolution. I've heard suggestions that we
should just backport it to C++23 as a nice quality of life improvement,
but I've also heard objections to any such feature backports (they hurt
portability between compilers). This just adds it for C++26.

-- >8 --

C++26 adds these convenience conversions.

libstdc++-v3/ChangeLog:

	* include/bits/version.def (to_chars): Define new value for
	C++26.
	* include/bits/version.h: Regenerate.
	* include/std/charconv (to_chars_result::operator bool): New
	function.
	(from_chars_result::operator bool): New function.
	* testsuite/20_util/to_chars/version.cc: Update expected value.
	* testsuite/20_util/from_chars/result.cc: New test.
	* testsuite/20_util/to_chars/result.cc: New test.
---
 libstdc++-v3/include/bits/version.def         |   7 +
 libstdc++-v3/include/bits/version.h           | 299 +++++++++---------
 libstdc++-v3/include/std/charconv             |   6 +
 .../testsuite/20_util/from_chars/result.cc    |  21 ++
 .../testsuite/20_util/to_chars/result.cc      |  25 ++
 .../testsuite/20_util/to_chars/version.cc     |   2 +-
 6 files changed, 212 insertions(+), 148 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/20_util/from_chars/result.cc
 create mode 100644 libstdc++-v3/testsuite/20_util/to_chars/result.cc

diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def
index 8d9b2f71a2e..b7d17c91c34 100644
--- a/libstdc++-v3/include/bits/version.def
+++ b/libstdc++-v3/include/bits/version.def
@@ -255,6 +255,13 @@ ftms = {
 
 ftms = {
   name = to_chars;
+  values = {
+    v = 202306;
+    cxxmin = 26;
+    extra_cond = "_GLIBCXX_FLOAT_IS_IEEE_BINARY32 "
+    "&& _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 "
+    "&& __SIZE_WIDTH__ >= 32";
+  };
   values = {
     v = 201611;
     cxxmin = 14;
diff --git a/libstdc++-v3/include/std/charconv b/libstdc++-v3/include/std/charconv
index 01711d38576..7edb6ade3d4 100644
--- a/libstdc++-v3/include/std/charconv
+++ b/libstdc++-v3/include/std/charconv
@@ -62,6 +62,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
     friend bool
     operator==(const to_chars_result&, const to_chars_result&) = default;
+#endif
+#if __cplusplus > 202302L
+    constexpr explicit operator bool() const noexcept { return ec == errc{}; }
 #endif
   };
 
@@ -74,6 +77,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
     friend bool
     operator==(const from_chars_result&, const from_chars_result&) = default;
+#endif
+#if __cplusplus > 202302L
+    constexpr explicit operator bool() const noexcept { return ec == errc{}; }
 #endif
   };
 
diff --git a/libstdc++-v3/testsuite/20_util/from_chars/result.cc b/libstdc++-v3/testsuite/20_util/from_chars/result.cc
new file mode 100644
index 00000000000..637678ac532
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/from_chars/result.cc
@@ -0,0 +1,21 @@
+// { dg-do run { target c++26 } }
+
+#include <charconv>
+#include <testsuite_hooks.h>
+
+void
+test_result()
+{
+  static_assert( ! std::is_convertible_v<std::from_chars_result, bool> );
+  static_assert( std::is_constructible_v<bool, std::from_chars_result> );
+
+  std::from_chars_result res{};
+  VERIFY( res );
+  res.ec = std::errc::invalid_argument;
+  VERIFY( !res );
+}
+
+int main()
+{
+  test_result();
+}
diff --git a/libstdc++-v3/testsuite/20_util/to_chars/result.cc b/libstdc++-v3/testsuite/20_util/to_chars/result.cc
new file mode 100644
index 00000000000..5284680460a
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/to_chars/result.cc
@@ -0,0 +1,25 @@
+// { dg-do run { target c++26 } }
+
+#include <charconv>
+#include <testsuite_hooks.h>
+
+#if defined(__cpp_lib_to_chars) && __cpp_lib_to_chars < 202306L
+# error "Feature-test macro for std::to_chars has wrong value in <charconv>"
+#endif
+
+void
+test_result()
+{
+  static_assert( ! std::is_convertible_v<std::to_chars_result, bool> );
+  static_assert( std::is_constructible_v<bool, std::to_chars_result> );
+
+  std::to_chars_result res{};
+  VERIFY( res );
+  res.ec = std::errc::invalid_argument;
+  VERIFY( !res );
+}
+
+int main()
+{
+  test_result();
+}
diff --git a/libstdc++-v3/testsuite/20_util/to_chars/version.cc b/libstdc++-v3/testsuite/20_util/to_chars/version.cc
index 2789afa28ef..f7866ec5377 100644
--- a/libstdc++-v3/testsuite/20_util/to_chars/version.cc
+++ b/libstdc++-v3/testsuite/20_util/to_chars/version.cc
@@ -6,7 +6,7 @@
 
 #ifndef __cpp_lib_to_chars
 # error "Feature-test macro for to_chars missing in <version>"
-#elif __cpp_lib_to_chars != 201611L
+#elif __cpp_lib_to_chars != (__cplusplus == 202302L ? 201611L : 202306L)
 # error "Feature-test macro for to_chars has wrong value in <version>"
 #endif
 
-- 
2.41.0


                 reply	other threads:[~2023-09-15  9:26 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=20230915092647.2192116-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).