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.
_M_construct(__str.begin(), __str.end());
}
+ basic_string(nullptr_t) = delete;
+ basic_string& operator=(nullptr_t) = delete;
#endif // C++11
/**
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
/**
#endif // C++23
#endif // C++20
+ basic_string_view(nullptr_t) = delete;
+
constexpr basic_string_view&
operator=(const basic_string_view&) noexcept = default;
--- /dev/null
+// { dg-do compile { target c++11 } }
+#include <string>
+
+std::string s = nullptr; // { dg-error "deleted" "P2166R1" }
{
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" }
}
{
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" }
}