Skip to content
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

[ranges] Apply resolution of LWG3502 to elements_view #1878

Merged
merged 4 commits into from
May 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
29 changes: 25 additions & 4 deletions stl/inc/ranges
Original file line number Diff line number Diff line change
Expand Up @@ -3701,10 +3701,14 @@ namespace ranges {
// clang-format on
};

template <class _Tuple, size_t _Index>
concept _Returnable_element = is_reference_v<_Tuple> || move_constructible<tuple_element_t<_Index, _Tuple>>;

// clang-format off
template <input_range _Vw, size_t _Index>
requires view<_Vw> && _Has_tuple_element<range_value_t<_Vw>, _Index>
&& _Has_tuple_element<remove_reference_t<range_reference_t<_Vw>>, _Index>
&& _Returnable_element<range_reference_t<_Vw>, _Index>
class elements_view : public view_interface<elements_view<_Vw, _Index>> {
// clang-format on
private:
Expand All @@ -3731,15 +3735,16 @@ namespace ranges {
template <bool>
friend class _Sentinel;

using _Base = _Maybe_const<_Const, _Vw>;
using _Base = _Maybe_const<_Const, _Vw>;
using _ElemTy = tuple_element_t<_Index, range_value_t<_Base>>;

iterator_t<_Base> _Current{};

public:
using iterator_concept = conditional_t<random_access_range<_Vw>, random_access_iterator_tag,
conditional_t<bidirectional_range<_Vw>, bidirectional_iterator_tag,
conditional_t<forward_range<_Vw>, forward_iterator_tag, input_iterator_tag>>>;
using value_type = remove_cvref_t<tuple_element_t<_Index, range_value_t<_Base>>>;
using value_type = remove_cvref_t<_ElemTy>;
using difference_type = range_difference_t<_Base>;

_Iterator() = default;
Expand Down Expand Up @@ -3767,10 +3772,16 @@ namespace ranges {
}

_NODISCARD constexpr decltype(auto) operator*() const
noexcept(noexcept(_STD get<_Index>(*_Current))) /* strengthened */ {
noexcept(noexcept(_STD get<_Index>(*_Current))) /* strengthened */
requires is_reference_v<range_reference_t<_Base>> {
return _STD get<_Index>(*_Current);
}

_NODISCARD constexpr decltype(auto) operator*() const noexcept(is_nothrow_move_constructible_v<_ElemTy> //
&& noexcept(_STD get<_Index>(*_Current))) /* strengthened */ {
return static_cast<remove_cv_t<_ElemTy>>(_STD get<_Index>(*_Current));
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
}

constexpr _Iterator& operator++() noexcept(noexcept(++_Current)) /* strengthened */ {
++_Current;
return *this;
Expand Down Expand Up @@ -3831,13 +3842,23 @@ namespace ranges {

_NODISCARD constexpr decltype(auto) operator[](const difference_type _Idx) const
noexcept(noexcept(_STD get<_Index>(*(_Current + _Idx)))) /* strengthened */
requires random_access_range<_Base> {
requires random_access_range<_Base>&& is_reference_v<range_reference_t<_Base>> {
#if _ITERATOR_DEBUG_LEVEL != 0
_Verify_offset(_Idx);
#endif // _ITERATOR_DEBUG_LEVEL != 0
return _STD get<_Index>(*(_Current + _Idx));
}

_NODISCARD constexpr decltype(auto) operator[](const difference_type _Idx) const
noexcept(is_nothrow_move_constructible_v<_ElemTy> //
&& noexcept(_STD get<_Index>(*(_Current + _Idx)))) /* strengthened */
requires random_access_range<_Base> {
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
#if _ITERATOR_DEBUG_LEVEL != 0
_Verify_offset(_Idx);
#endif // _ITERATOR_DEBUG_LEVEL != 0
return static_cast<remove_cv_t<_ElemTy>>(_STD get<_Index>(*(_Current + _Idx)));
}

_NODISCARD friend constexpr bool operator==(const _Iterator& _Left, const _Iterator& _Right) noexcept(
noexcept(_Left._Current == _Right._Current)) /* strengthened */
requires equality_comparable<iterator_t<_Base>> {
Expand Down
7 changes: 7 additions & 0 deletions tests/std/tests/P0896R4_views_elements/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,11 @@ int main() {
STATIC_ASSERT(test_one(s));
test_one(s);
}

{ // Validate a view borrowed range
constexpr auto v = views::iota(0ull, ranges::size(expected_keys))
| views::transform([](auto i) { return make_pair(expected_keys[i], expected_values[i]); });
STATIC_ASSERT(test_one(v));
test_one(v);
}
}