Skip to content

Commit

Permalink
P2166R1 Prohibiting basic_string And basic_string_view Construction F…
Browse files Browse the repository at this point in the history
…rom nullptr (#1995)

Co-authored-by: Casey Carter <cacarter@microsoft.com>
Co-authored-by: Stephan T. Lavavej <stl@nuwen.net>
  • Loading branch information
3 people authored Jun 29, 2021
1 parent d695966 commit a8c7283
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions stl/inc/xstring
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,10 @@ public:
/* implicit */ constexpr basic_string_view(_In_z_ const const_pointer _Ntcts) noexcept // strengthened
: _Mydata(_Ntcts), _Mysize(_Traits::length(_Ntcts)) {}

#if _HAS_CXX23
basic_string_view(nullptr_t) = delete;
#endif // _HAS_CXX23

constexpr basic_string_view(
_In_reads_(_Count) const const_pointer _Cts, const size_type _Count) noexcept // strengthened
: _Mydata(_Cts), _Mysize(_Count) {
Expand Down Expand Up @@ -2536,6 +2540,10 @@ public:
_Proxy._Release();
}

#if _HAS_CXX23
basic_string(nullptr_t) = delete;
#endif // _HAS_CXX23

#if _HAS_CXX17
template <class _Alloc2 = _Alloc, enable_if_t<_Is_allocator<_Alloc2>::value, int> = 0>
#endif // _HAS_CXX17
Expand Down Expand Up @@ -3106,6 +3114,10 @@ public:
return assign(_Ptr);
}

#if _HAS_CXX23
basic_string& operator=(nullptr_t) = delete;
#endif // _HAS_CXX23

_CONSTEXPR20_CONTAINER basic_string& operator=(const _Elem _Ch) { // assign {_Ch, _Elem()}
_Mypair._Myval2._Mysize = 1;
_Elem* const _Ptr = _Mypair._Myval2._Myptr();
Expand Down
1 change: 1 addition & 0 deletions stl/inc/yvals_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
// P1048R1 is_scoped_enum
// P1679R3 contains() For basic_string/basic_string_view
// P1682R3 to_underlying() For Enumerations
// P2166R1 Prohibiting basic_string And basic_string_view Construction From nullptr

// Parallel Algorithms Notes
// C++ allows an implementation to implement parallel algorithms as calls to the serial algorithms.
Expand Down
8 changes: 8 additions & 0 deletions tests/std/tests/P0220R1_string_view/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string>
#include <string_view>
#include <type_traits>

#include <constexpr_char_traits.hpp>

Expand Down Expand Up @@ -1199,6 +1200,13 @@ static_assert(u8"abc"sv[1] == u8'b');
static_assert(noexcept(u8"abc"sv));
#endif // __cpp_char8_t

// P2166R1 Prohibiting basic_string And basic_string_view Construction From nullptr
#if _HAS_CXX23
static_assert(!is_constructible_v<string_view, nullptr_t>, "constructing string_view from nullptr_t is prohibited");
static_assert(!is_constructible_v<string, nullptr_t>, "constructing string from nullptr_t is prohibited");
static_assert(!is_assignable_v<string&, nullptr_t>, "assigning nullptr_t to string is prohibited");
#endif // _HAS_CXX23

int main() {
test_case_default_constructor();
test_case_ntcts_constructor();
Expand Down

0 comments on commit a8c7283

Please sign in to comment.