libstdc++: Limit new basic_string(nullptr_t) constructor to C++23 [PR104099]
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.
This commit is contained in:
parent
3c4a54adb2
commit
fe3ed885cd
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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 }
|
||||
}
|
||||
|
@ -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 }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user