libstdc++: Disable std::string{,_view} construction from nullptr (P2166R1)
Implement this C++23 feature. Because construction from a null pointer is undefined, we can implement it for C++11 and up, turning undefined behaviour into a compilation error. libstdc++-v3/ChangeLog: * include/bits/basic_string.h (basic_string(nullptr_t)): Define as deleted. (operator=(nullptr_t)): Likewise. * include/bits/cow_string.h (basic_string(nullptr_t)): Likewise. (operator=(nullptr_t)): Likewise. * include/std/string_view (basic_string_view(nullptr_t)): Likewise. * testsuite/21_strings/basic_string/cons/char/nullptr.cc: New test. * testsuite/21_strings/basic_string_view/cons/char/nonnull.cc: Change dg-warning to dg-error. * testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc: Likewise.
This commit is contained in:
parent
fa3ccf8bfe
commit
cf876562c5
@ -623,6 +623,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
|
||||
_M_construct(__str.begin(), __str.end());
|
||||
}
|
||||
|
||||
basic_string(nullptr_t) = delete;
|
||||
basic_string& operator=(nullptr_t) = delete;
|
||||
#endif // C++11
|
||||
|
||||
/**
|
||||
|
@ -667,6 +667,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
else
|
||||
_M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a);
|
||||
}
|
||||
|
||||
basic_string(nullptr_t) = delete;
|
||||
basic_string& operator=(nullptr_t) = delete;
|
||||
#endif // C++11
|
||||
|
||||
/**
|
||||
|
@ -168,6 +168,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
||||
#endif // C++23
|
||||
#endif // C++20
|
||||
|
||||
basic_string_view(nullptr_t) = delete;
|
||||
|
||||
constexpr basic_string_view&
|
||||
operator=(const basic_string_view&) noexcept = default;
|
||||
|
||||
|
@ -0,0 +1,4 @@
|
||||
// { dg-do compile { target c++11 } }
|
||||
#include <string>
|
||||
|
||||
std::string s = nullptr; // { dg-error "deleted" "P2166R1" }
|
@ -25,5 +25,5 @@ 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-warning "\\\[-Wnonnull" }
|
||||
std::string_view u(nullptr); // { dg-error "deleted" }
|
||||
}
|
||||
|
@ -25,5 +25,5 @@ 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-warning "\\\[-Wnonnull" }
|
||||
std::wstring_view u(nullptr); // { dg-error "deleted" }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user