[libc++] [c++2b] [P2166] Prohibit string and string_view construction from nullptr.

* https://wg21.link/P2166

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D106801
This commit is contained in:
Marek Kurdej 2021-07-27 16:16:21 +02:00
parent 5ea091a817
commit 775caa58fc
6 changed files with 66 additions and 7 deletions

View File

@ -21,5 +21,5 @@
"`P1951R1 <https://wg21.link/P1951R1>`__","LWG","Default Arguments for pair Forwarding Constructor","June 2021","",""
"`P1989R2 <https://wg21.link/P1989R2>`__","LWG","Range constructor for std::string_view","June 2021","",""
"`P2136R3 <https://wg21.link/P2136R3>`__","LWG","invoke_r","June 2021","",""
"`P2166R1 <https://wg21.link/P2166R1>`__","LWG","A Proposal to Prohibit std::basic_string and std::basic_string_view construction from nullptr","June 2021","",""
"`P2166R1 <https://wg21.link/P2166R1>`__","LWG","A Proposal to Prohibit std::basic_string and std::basic_string_view construction from nullptr","June 2021","|Complete|","13.0"
"","","","","",""
1 Paper # Group Paper Name Meeting Status First released version
21 `P1951R1 <https://wg21.link/P1951R1>`__ LWG Default Arguments for pair Forwarding Constructor June 2021
22 `P1989R2 <https://wg21.link/P1989R2>`__ LWG Range constructor for std::string_view June 2021
23 `P2136R3 <https://wg21.link/P2136R3>`__ LWG invoke_r June 2021
24 `P2166R1 <https://wg21.link/P2166R1>`__ LWG A Proposal to Prohibit std::basic_string and std::basic_string_view construction from nullptr June 2021 |Complete| 13.0
25

View File

@ -110,6 +110,7 @@ public:
explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
basic_string(const value_type* s, const allocator_type& a = allocator_type());
basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
basic_string(nullptr_t) = delete; // C++2b
basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
template<class InputIterator>
basic_string(InputIterator begin, InputIterator end,
@ -130,6 +131,7 @@ public:
allocator_type::propagate_on_container_move_assignment::value ||
allocator_type::is_always_equal::value ); // C++17
basic_string& operator=(const value_type* s);
basic_string& operator=(nullptr_t) = delete; // C++2b
basic_string& operator=(value_type c);
basic_string& operator=(initializer_list<value_type>);
@ -843,6 +845,10 @@ public:
_LIBCPP_INLINE_VISIBILITY
basic_string(const _CharT* __s, const _Allocator& __a);
#if _LIBCPP_STD_VER > 20
basic_string(nullptr_t) = delete;
#endif
_LIBCPP_INLINE_VISIBILITY
basic_string(const _CharT* __s, size_type __n);
_LIBCPP_INLINE_VISIBILITY
@ -906,6 +912,9 @@ public:
basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
#endif
_LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
#if _LIBCPP_STD_VER > 20
basic_string& operator=(nullptr_t) = delete;
#endif
basic_string& operator=(value_type __c);
#if _LIBCPP_DEBUG_LEVEL == 2

View File

@ -83,6 +83,7 @@ namespace std {
basic_string_view& operator=(const basic_string_view&) noexcept = default;
template<class Allocator>
constexpr basic_string_view(const charT* str);
basic_string_view(nullptr_t) = delete; // C++2b
constexpr basic_string_view(const charT* str, size_type len);
// 7.4, basic_string_view iterator support
@ -273,6 +274,10 @@ public:
basic_string_view(const _CharT* __s)
: __data(__s), __size(_VSTD::__char_traits_length_checked<_Traits>(__s)) {}
#if _LIBCPP_STD_VER > 20
basic_string_view(nullptr_t) = delete;
#endif
// [string.view.iterators], iterators
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
const_iterator begin() const _NOEXCEPT { return cbegin(); }

View File

@ -21,18 +21,22 @@
#include "debug_mode_helper.h"
void test_null_argument() {
EXPECT_DEATH(std::string_view(nullptr));
EXPECT_DEATH(std::string_view(NULL));
// C++2b prohibits construction of string_view from nullptr_t.
const char* nullp = nullptr;
const char* null = NULL;
(void)nullp;
(void)null;
EXPECT_DEATH((std::string_view(nullp)));
EXPECT_DEATH((std::string_view(null)));
EXPECT_DEATH(std::string_view(static_cast<const char*>(0)));
{
std::string_view v;
EXPECT_DEATH(((void)(v == nullptr)));
EXPECT_DEATH(((void)(nullptr == v)));
EXPECT_DEATH(((void)(v == nullp)));
EXPECT_DEATH(((void)(nullp == v)));
}
}
int main(int, char**)
{
int main(int, char**) {
test_null_argument();
return 0;

View File

@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// 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
// <string>
// basic_string(nullptr_t) = delete; // C++2b
// basic_string& operator=(nullptr_t) = delete; // C++2b
#include <string>
#include <type_traits>
static_assert(!std::is_convertible_v<decltype(nullptr), std::string>);
static_assert(!std::is_constructible_v<std::string, decltype(nullptr)>);
static_assert(!std::is_assignable_v<std::string, decltype(nullptr)>);

View File

@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// 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
// <string_view>
// basic_string_view(nullptr_t) = delete; // C++2b
#include <string_view>
#include <type_traits>
static_assert(!std::is_convertible_v<decltype(nullptr), std::string_view>);
static_assert(!std::is_constructible_v<std::string_view, decltype(nullptr)>);
static_assert(!std::is_assignable_v<std::string_view, decltype(nullptr)>);