libstdc++: Limit new basic_string(nullptr_t) constructor to C++23 [PR104099]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 18 Jan 2022 16:26:45 +0000 (16:26 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 18 Jan 2022 20:41:46 +0000 (20:41 +0000)
The new deleted constructors added by P2166R1 are a breaking change,
making previously valid code ill-formed in C++23. As a result, they
should only be defined for C++23 and not for C++11 and up.

libstdc++-v3/ChangeLog:

PR libstdc++/104099
* include/bits/basic_string.h (basic_string(nullptr_t)): Only
define for C++23.
(operator=(nullptr_t)): Likewise.
* include/bits/cow_string.h: Likewise.
* include/std/string_view (basic_string_view(nullptr_t)):
Likewise.
* testsuite/21_strings/basic_string/cons/char/nullptr.cc: Adjust
expected error. Add examples that become ill-formed in C++23.
* testsuite/21_strings/basic_string_view/cons/char/nonnull.cc:
Adjust expected errors.
* testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc:
Likewise.

libstdc++-v3/include/bits/basic_string.h
libstdc++-v3/include/bits/cow_string.h
libstdc++-v3/include/std/string_view
libstdc++-v3/testsuite/21_strings/basic_string/cons/char/nullptr.cc
libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc
libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc

index a91ba51..fc6a303 100644 (file)
@@ -728,10 +728,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
        else
          _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag());
       }
+#endif // C++11
 
+#if __cplusplus >= 202100L
       basic_string(nullptr_t) = delete;
       basic_string& operator=(nullptr_t) = delete;
-#endif // C++11
+#endif // C++23
 
       /**
        *  @brief  Construct string as copy of a range.
index 84aab2f..a49a5b0 100644 (file)
@@ -665,10 +665,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        else
          _M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a);
       }
+#endif // C++11
 
+#if __cplusplus >= 202100L
       basic_string(nullptr_t) = delete;
       basic_string& operator=(nullptr_t) = delete;
-#endif // C++11
+#endif // C++23
 
       /**
        *  @brief  Construct string as copy of a range.
index 99080e9..bccf4d1 100644 (file)
@@ -167,10 +167,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        noexcept(noexcept(ranges::size(__r)) && noexcept(ranges::data(__r)))
        : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
        { }
+
+      basic_string_view(nullptr_t) = delete;
 #endif // C++23
 #endif // C++20
 
-      basic_string_view(nullptr_t) = delete;
 
       constexpr basic_string_view&
       operator=(const basic_string_view&) noexcept = default;
index fdb24ae..a69fa61 100644 (file)
@@ -1,4 +1,28 @@
 // { dg-do compile { target c++11 } }
 #include <string>
 
-std::string s = nullptr; // { dg-error "deleted" "P2166R1" }
+std::string s = nullptr; // { dg-error "deleted" "P2166R1" { target c++23 } }
+
+struct S
+{
+  operator const char*() const { return ""; }
+  operator std::nullptr_t() const { return {}; }
+};
+
+std::string s2{ S{} }; // { dg-error "deleted" "PR 104099" { target c++23 } }
+
+#if __cpp_concepts
+struct J
+{
+  // In C++20 this selects basic_string(const char*),
+  // in C++23 it's ambiguous due to basic_string(nullptr_t).
+  template<typename T>
+    requires (!std::is_same_v<std::allocator<char>, T>)
+    && (!std::is_same_v<std::string, T>)
+    && (!std::is_same_v<char, T>)
+    && (!std::is_same_v<std::string_view, T>)
+    operator T() const { return {}; }
+};
+
+std::string s3{ J{} }; // { dg-error "ambiguous" "PR 104099" { target c++23 } }
+#endif
index 00bb8e4..2e43788 100644 (file)
@@ -25,5 +25,6 @@ test01()
 {
   std::string_view s((const char*)nullptr); // { dg-warning "\\\[-Wnonnull" }
   std::string_view t((char*)nullptr);      // { dg-warning "\\\[-Wnonnull" }
-  std::string_view u(nullptr);             // { dg-error "deleted" }
+  std::string_view u(nullptr);             // { dg-warning "\\\[-Wnonnull" "" { target c++20_down } }
+// { dg-error "deleted" "P2166R1" { target c++23 } 0 }
 }
index 685d48c..a146d38 100644 (file)
@@ -25,5 +25,6 @@ test01()
 {
   std::wstring_view s((const wchar_t*)nullptr);        // { dg-warning "\\\[-Wnonnull" }
   std::wstring_view t((wchar_t*)nullptr);      // { dg-warning "\\\[-Wnonnull" }
-  std::wstring_view u(nullptr);                        // { dg-error "deleted" }
+  std::wstring_view u(nullptr);                        // { dg-warning "\\\[-Wnonnull" "" { target c++20_down } }
+// { dg-error "deleted" "P2166R1" { target c++23 } 0 }
 }