Skip to content

P1989R2 Range Constructor For string_view #2000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b2d12fa
P1989R2 Range Constructor For `string_view`
sam20908 Jun 10, 2021
93cf217
Move helper concept to above `basic_string_view`
sam20908 Jun 10, 2021
8986a22
Fixed syntax errors
sam20908 Jun 10, 2021
6739fb8
Reorder ranges ctor according tot he Standard
sam20908 Jun 10, 2021
ba7ee27
Guard against EDG
sam20908 Jun 10, 2021
7601af6
Add comment indicating non-WP fix
sam20908 Jun 11, 2021
94f0382
Oops, forgot to remove the helper concept
sam20908 Jun 11, 2021
c2519ee
Added deduction guides
sam20908 Jun 11, 2021
d93f9c3
Reorder ranges dtor to be within existing __cpp_lib_concepts guard
sam20908 Jun 11, 2021
00d8cbb
Fixed traits constraint
sam20908 Jun 11, 2021
5e695c4
Added test
sam20908 Jun 11, 2021
26631fb
Add _HAS_CXX23 directly controls note
sam20908 Jun 11, 2021
98e2d8a
Address review comments
sam20908 Jun 11, 2021
14a9d91
Added constexpr test and removed redundant lines
sam20908 Jun 11, 2021
1e1be9e
Removed unnecessary include and cast to `std::size_t`
sam20908 Jun 11, 2021
c1d31c0
Merge branch 'cxx23_string_view_range_ctor' of https://github.com/sam…
sam20908 Jun 11, 2021
10de48d
Update stl/inc/xstring
CaseyCarter Jun 11, 2021
748f20d
Merge branch 'main' into cxx23_string_view_range_ctor
sam20908 Jun 12, 2021
c588be2
Reorder test_case_range_constructor's definition and static_assert.
StephanTLavavej Jun 17, 2021
1f1a77a
Add constraint tests.
StephanTLavavej Jun 17, 2021
6fe08bb
Merge branch 'main' into cxx23_string_view_range_ctor
StephanTLavavej Jun 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions stl/inc/xstring
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,25 @@ public:
constexpr basic_string_view(_It _First, _Se _Last) noexcept(noexcept(_Last - _First)) // strengthened
: _Mydata(_STD to_address(_First)), _Mysize(static_cast<size_type>(_Last - _First)) {}
// clang-format on

#if _HAS_CXX23
// clang-format off
template <_RANGES contiguous_range _Range>
requires _RANGES sized_range<_Range>
&& same_as<_RANGES range_value_t<_Range>, _Elem>
&& (!is_convertible_v<_Range, const _Elem*>)
&& (!requires(remove_cvref_t<_Range>& _Rng) {
_Rng.operator _STD basic_string_view<_Elem, _Traits>();
})
&& (!requires {
// per editorial or LWG issue not yet filed as of 2021-06-10
typename remove_reference_t<_Range>::traits_type;
} || same_as<typename remove_reference_t<_Range>::traits_type, _Traits>)
constexpr basic_string_view(_Range&& _Rng) noexcept(
noexcept(_RANGES data(_Rng)) && noexcept(_RANGES size(_Rng))) // strengthened
: _Mydata(_RANGES data(_Rng)), _Mysize(static_cast<size_t>(_RANGES size(_Rng))) {}
// clang-format on
#endif // _HAS_CXX23
#endif // __cpp_lib_concepts

_NODISCARD constexpr const_iterator begin() const noexcept {
Expand Down Expand Up @@ -1696,6 +1715,11 @@ private:
template <contiguous_iterator _It, sized_sentinel_for<_It> _Se>
basic_string_view(_It, _Se) -> basic_string_view<iter_value_t<_It>>;

#if _HAS_CXX23
template <_RANGES contiguous_range _Range>
basic_string_view(_Range&&) -> basic_string_view<_RANGES range_value_t<_Range>>;
#endif // _HAS_CXX23

namespace ranges {
template <class _Elem, class _Traits>
inline constexpr bool enable_view<basic_string_view<_Elem, _Traits>> = true;
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 @@ -263,6 +263,7 @@
// P1048R1 is_scoped_enum
// P1679R3 contains() For basic_string/basic_string_view
// P1682R3 to_underlying() For Enumerations
// P1989R2 Range Constructor For string_view
// P2166R1 Prohibiting basic_string And basic_string_view Construction From nullptr

// Parallel Algorithms Notes
Expand Down
29 changes: 29 additions & 0 deletions tests/std/tests/P0220R1_string_view/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

#include <array>
#include <assert.h>
#include <deque>
#include <sstream>
#include <stdexcept>
#include <stdlib.h>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>

#include <constexpr_char_traits.hpp>

Expand Down Expand Up @@ -323,6 +325,31 @@ constexpr bool test_case_contiguous_constructor() {
return true;
}

constexpr bool test_case_range_constructor() {
#if _HAS_CXX23 && defined(__cpp_lib_concepts)
const array expectedData{'n', 'o', ' ', 'n', 'u', 'l', 'l'};
// Also tests the corresponding deduction guide:
same_as<string_view> auto sv = basic_string_view(expectedData);
assert(sv.data() == expectedData.data());
assert(sv.size() == 7);

// Also tests some of the constraints:
static_assert(is_constructible_v<string_view, vector<char>>);
static_assert(is_convertible_v<vector<char>, string_view>);

static_assert(!is_constructible_v<string_view, deque<char>>); // not contiguous
static_assert(!is_convertible_v<deque<char>, string_view>);

static_assert(!is_constructible_v<string_view, vector<unsigned char>>); // different elements
static_assert(!is_convertible_v<vector<unsigned char>, string_view>);

static_assert(!is_constructible_v<string_view, basic_string<char, constexpr_char_traits>>); // different traits
static_assert(!is_convertible_v<basic_string<char, constexpr_char_traits>, string_view>);
#endif // _HAS_CXX23 && defined(__cpp_lib_concepts)

return true;
}

template <class CharT, class Traits>
constexpr bool test_case_iterators() {
using iterator = typename basic_string_view<CharT, Traits>::iterator;
Expand Down Expand Up @@ -1157,6 +1184,7 @@ static_assert(test_case_default_constructor());
static_assert(test_case_ntcts_constructor<constexpr_char_traits>());
static_assert(test_case_buffer_constructor());
static_assert(test_case_contiguous_constructor());
static_assert(test_case_range_constructor());
#if defined(__clang__) || defined(__EDG__) // TRANSITION, VSO-284079 "C1XX's C++14 constexpr emits bogus warnings C4146,
// C4308, C4307 for basic_string_view::iterator"
static_assert(test_case_iterators<char, constexpr_char_traits>());
Expand Down Expand Up @@ -1212,6 +1240,7 @@ int main() {
test_case_ntcts_constructor();
test_case_buffer_constructor();
test_case_contiguous_constructor();
test_case_range_constructor();
test_case_iterators<char, char_traits<char>>();
test_case_iterators<wchar_t, char_traits<wchar_t>>();
test_case_prefix<char, char_traits<char>>();
Expand Down