diff mbox series

[committed] libstdc++: Add operator bool to <charconv> result types (P2497R0)

Message ID 20230915092647.2192116-1-jwakely@redhat.com
State New
Headers show
Series [committed] libstdc++: Add operator bool to <charconv> result types (P2497R0) | expand

Commit Message

Jonathan Wakely Sept. 15, 2023, 9:24 a.m. UTC
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 mbox series

Patch

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