[libcxx-commits] [libcxx] 92ac360 - [libc++][charconv] Adds operator bool.

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 23 10:25:17 PDT 2023


Author: Mark de Wever
Date: 2023-08-23T19:25:10+02:00
New Revision: 92ac3600630c63863467112b2f072753d84bcf78

URL: https://github.com/llvm/llvm-project/commit/92ac3600630c63863467112b2f072753d84bcf78
DIFF: https://github.com/llvm/llvm-project/commit/92ac3600630c63863467112b2f072753d84bcf78.diff

LOG: [libc++][charconv] Adds operator bool.

Implements
- P2497R0 Testing for success or failure of <charconv> functions

Depends on D153192

Reviewed By: #libc, ldionne

Differential Revision: https://reviews.llvm.org/D153199

Added: 
    libcxx/test/std/utilities/charconv/charconv.syn/from_chars_result.operator_bool.pass.cpp
    libcxx/test/std/utilities/charconv/charconv.syn/to_chars_result.operator_bool.pass.cpp

Modified: 
    libcxx/docs/FeatureTestMacroTable.rst
    libcxx/docs/ReleaseNotes/18.rst
    libcxx/docs/Status/Cxx2cPapers.csv
    libcxx/include/__charconv/from_chars_result.h
    libcxx/include/__charconv/to_chars_result.h
    libcxx/include/charconv
    libcxx/include/version
    libcxx/test/std/language.support/support.limits/support.limits.general/charconv.version.compile.pass.cpp
    libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
    libcxx/utils/generate_feature_test_macro_components.py

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst
index 1fdc27b8b97da0..8b35dc36b76fd8 100644
--- a/libcxx/docs/FeatureTestMacroTable.rst
+++ b/libcxx/docs/FeatureTestMacroTable.rst
@@ -410,6 +410,8 @@ Status
     --------------------------------------------------- -----------------
     ``__cpp_lib_text_encoding``                         *unimplemented*
     --------------------------------------------------- -----------------
+    ``__cpp_lib_to_chars``                              *unimplemented*
+    --------------------------------------------------- -----------------
     ``__cpp_lib_within_lifetime``                       *unimplemented*
     =================================================== =================
 

diff  --git a/libcxx/docs/ReleaseNotes/18.rst b/libcxx/docs/ReleaseNotes/18.rst
index 4050119d64db36..613a07f2e3560e 100644
--- a/libcxx/docs/ReleaseNotes/18.rst
+++ b/libcxx/docs/ReleaseNotes/18.rst
@@ -38,6 +38,7 @@ What's New in Libc++ 18.0.0?
 
 Implemented Papers
 ------------------
+- P2497R0 - Testing for success or failure of ``<charconv>`` functions
 
 
 Improvements and New Features

diff  --git a/libcxx/docs/Status/Cxx2cPapers.csv b/libcxx/docs/Status/Cxx2cPapers.csv
index f00a123bd85a25..3a8bee95f07333 100644
--- a/libcxx/docs/Status/Cxx2cPapers.csv
+++ b/libcxx/docs/Status/Cxx2cPapers.csv
@@ -1,5 +1,5 @@
 "Paper #","Group","Paper Name","Meeting","Status","First released version","Labels"
-"`P2497R0 <https://wg21.link/P2497R0>`__","LWG","Testing for success or failure of ``<charconv>`` functions","Varna June 2023","","",""
+"`P2497R0 <https://wg21.link/P2497R0>`__","LWG","Testing for success or failure of ``<charconv>`` functions","Varna June 2023","|Complete|","18.0",""
 "`P2592R3 <https://wg21.link/P2592R3>`__","LWG","Hashing support for ``std::chrono`` value classes","Varna June 2023","","",""
 "`P2587R3 <https://wg21.link/P2587R3>`__","LWG","``to_string`` or not ``to_string``","Varna June 2023","","","|format|"
 "`P2562R1 <https://wg21.link/P2562R1>`__","LWG","``constexpr`` Stable Sorting","Varna June 2023","","",""

diff  --git a/libcxx/include/__charconv/from_chars_result.h b/libcxx/include/__charconv/from_chars_result.h
index fa98616a0cb762..a7bfd6530a8a0e 100644
--- a/libcxx/include/__charconv/from_chars_result.h
+++ b/libcxx/include/__charconv/from_chars_result.h
@@ -27,6 +27,9 @@ struct _LIBCPP_EXPORTED_FROM_ABI from_chars_result {
 #  if _LIBCPP_STD_VER >= 20
   _LIBCPP_HIDE_FROM_ABI friend bool operator==(const from_chars_result&, const from_chars_result&) = default;
 #  endif
+#  if _LIBCPP_STD_VER >= 26
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return ec == errc{}; }
+#  endif
 };
 
 #endif // _LIBCPP_STD_VER >= 17

diff  --git a/libcxx/include/__charconv/to_chars_result.h b/libcxx/include/__charconv/to_chars_result.h
index aba5e5f06b5bf6..8df0897a49fbbd 100644
--- a/libcxx/include/__charconv/to_chars_result.h
+++ b/libcxx/include/__charconv/to_chars_result.h
@@ -27,6 +27,9 @@ struct _LIBCPP_EXPORTED_FROM_ABI to_chars_result {
 #  if _LIBCPP_STD_VER >= 20
   _LIBCPP_HIDE_FROM_ABI friend bool operator==(const to_chars_result&, const to_chars_result&) = default;
 #  endif
+#  if _LIBCPP_STD_VER >= 26
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return ec == errc{}; }
+#  endif
 };
 
 #endif // _LIBCPP_STD_VER >= 17

diff  --git a/libcxx/include/charconv b/libcxx/include/charconv
index 5b6c72ea7f101d..92273b4ce4766b 100644
--- a/libcxx/include/charconv
+++ b/libcxx/include/charconv
@@ -28,6 +28,7 @@ namespace std {
     char* ptr;
     errc ec;
     friend bool operator==(const to_chars_result&, const to_chars_result&) = default; // since C++20
+    constexpr explicit operator bool() const noexcept { return ec == errc{}; }        // since C++26
   };
 
   constexpr to_chars_result to_chars(char* first, char* last, see below value,
@@ -58,6 +59,7 @@ namespace std {
     const char* ptr;
     errc ec;
     friend bool operator==(const from_chars_result&, const from_chars_result&) = default; // since C++20
+    constexpr explicit operator bool() const noexcept { return ec == errc{}; }            // since C++26
   };
 
   constexpr from_chars_result from_chars(const char* first, const char* last,

diff  --git a/libcxx/include/version b/libcxx/include/version
index 53d40e7bb2b1db..885af1ba10da1d 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -203,7 +203,8 @@ __cpp_lib_text_encoding                                 202306L <text_encoding>
 __cpp_lib_three_way_comparison                          201907L <compare>
 __cpp_lib_to_address                                    201711L <memory>
 __cpp_lib_to_array                                      201907L <array>
-__cpp_lib_to_chars                                      201611L <charconv>
+__cpp_lib_to_chars                                      202306L <charconv>
+                                                        201611L // C++17
 __cpp_lib_to_string                                     202306L <string>
 __cpp_lib_to_underlying                                 202102L <utility>
 __cpp_lib_transformation_trait_aliases                  201304L <type_traits>
@@ -470,6 +471,8 @@ __cpp_lib_within_lifetime                               202306L <type_traits>
 // # define __cpp_lib_sstream_from_string_view             202306L
 // # define __cpp_lib_submdspan                            202306L
 // # define __cpp_lib_text_encoding                        202306L
+# undef  __cpp_lib_to_chars
+// # define __cpp_lib_to_chars                             202306L
 // # define __cpp_lib_within_lifetime                      202306L
 #endif
 

diff  --git a/libcxx/test/std/language.support/support.limits/support.limits.general/charconv.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/charconv.version.compile.pass.cpp
index 8263ae8f819bbe..cc38cbacd51b73 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/charconv.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/charconv.version.compile.pass.cpp
@@ -18,6 +18,7 @@
 /*  Constant                        Value
     __cpp_lib_constexpr_charconv    202207L [C++23]
     __cpp_lib_to_chars              201611L [C++17]
+                                    202306L [C++26]
 */
 
 #include <charconv>
@@ -116,8 +117,8 @@
 #   ifndef __cpp_lib_to_chars
 #     error "__cpp_lib_to_chars should be defined in c++26"
 #   endif
-#   if __cpp_lib_to_chars != 201611L
-#     error "__cpp_lib_to_chars should have the value 201611L in c++26"
+#   if __cpp_lib_to_chars != 202306L
+#     error "__cpp_lib_to_chars should have the value 202306L in c++26"
 #   endif
 # else // _LIBCPP_VERSION
 #   ifdef __cpp_lib_to_chars

diff  --git a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
index ec226f3a5fa055..74df2ef7b6b13f 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
@@ -189,6 +189,7 @@
     __cpp_lib_to_address                             201711L [C++20]
     __cpp_lib_to_array                               201907L [C++20]
     __cpp_lib_to_chars                               201611L [C++17]
+                                                     202306L [C++26]
     __cpp_lib_to_string                              202306L [C++23]
     __cpp_lib_to_underlying                          202102L [C++23]
     __cpp_lib_transformation_trait_aliases           201304L [C++14]
@@ -6954,8 +6955,8 @@
 #   ifndef __cpp_lib_to_chars
 #     error "__cpp_lib_to_chars should be defined in c++26"
 #   endif
-#   if __cpp_lib_to_chars != 201611L
-#     error "__cpp_lib_to_chars should have the value 201611L in c++26"
+#   if __cpp_lib_to_chars != 202306L
+#     error "__cpp_lib_to_chars should have the value 202306L in c++26"
 #   endif
 # else // _LIBCPP_VERSION
 #   ifdef __cpp_lib_to_chars

diff  --git a/libcxx/test/std/utilities/charconv/charconv.syn/from_chars_result.operator_bool.pass.cpp b/libcxx/test/std/utilities/charconv/charconv.syn/from_chars_result.operator_bool.pass.cpp
new file mode 100644
index 00000000000000..b628a2c76acf50
--- /dev/null
+++ b/libcxx/test/std/utilities/charconv/charconv.syn/from_chars_result.operator_bool.pass.cpp
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <charconv>
+
+// struct from_chars_result
+//   constexpr explicit operator bool() const noexcept { return ec == errc{}; }
+
+#include <charconv>
+
+#include <cassert>
+#include <type_traits>
+
+#include "test_macros.h"
+
+static_assert(!std::is_convertible_v<std::from_chars_result, bool>);
+static_assert(std::is_constructible_v<bool, std::from_chars_result>);
+
+constexpr bool test() {
+  // True
+  {
+    std::from_chars_result value{nullptr, std::errc{}};
+    assert(bool(value) == true);
+    static_assert(noexcept(bool(true)) == true);
+  }
+  // False
+  {
+    std::from_chars_result value{nullptr, std::errc::value_too_large};
+    assert(bool(value) == false);
+    static_assert(noexcept(bool(true)) == true);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  assert(test());
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/test/std/utilities/charconv/charconv.syn/to_chars_result.operator_bool.pass.cpp b/libcxx/test/std/utilities/charconv/charconv.syn/to_chars_result.operator_bool.pass.cpp
new file mode 100644
index 00000000000000..ef9364d3a64709
--- /dev/null
+++ b/libcxx/test/std/utilities/charconv/charconv.syn/to_chars_result.operator_bool.pass.cpp
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <charconv>
+
+// struct to_chars_result
+//   constexpr explicit operator bool() const noexcept { return ec == errc{}; }
+
+#include <charconv>
+
+#include <cassert>
+#include <type_traits>
+
+#include "test_macros.h"
+
+static_assert(!std::is_convertible_v<std::to_chars_result, bool>);
+static_assert(std::is_constructible_v<bool, std::to_chars_result>);
+
+constexpr bool test() {
+  // True
+  {
+    std::to_chars_result value{nullptr, std::errc{}};
+    assert(bool(value) == true);
+    static_assert(noexcept(bool(true)) == true);
+  }
+  // False
+  {
+    std::to_chars_result value{nullptr, std::errc::value_too_large};
+    assert(bool(value) == false);
+    static_assert(noexcept(bool(true)) == true);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  assert(test());
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index 1d47d2c519d69c..ce7df8429907fc 100755
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -1055,7 +1055,7 @@ def add_version_header(tc):
             "name": "__cpp_lib_to_chars",
             "values": {
                          "c++17": 201611,
-                         #"c++26: 202306, # P2497R0 Testing for success or failure of <charconv> functions
+                         "c++26": 202306, # P2497R0 Testing for success or failure of <charconv> functions
                       },
             "headers": ["charconv"],
             "unimplemented": True,


        


More information about the libcxx-commits mailing list