Skip to content

Commit

Permalink
[ranges] Apply resolution of LWG3502 to elements_view
Browse files Browse the repository at this point in the history
fixes #1685
  • Loading branch information
miscco committed Apr 23, 2021
1 parent f675d68 commit a7965e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 16 additions & 2 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 Down Expand Up @@ -3768,7 +3772,12 @@ namespace ranges {

_NODISCARD constexpr decltype(auto) operator*() const
noexcept(noexcept(_STD get<_Index>(*_Current))) /* strengthened */ {
return _STD get<_Index>(*_Current);
if constexpr (is_reference_v<range_reference_t<_Base>>) {
return _STD get<_Index>(*_Current);
} else {
using _ElemTy = remove_cv_t<tuple_element_t<_Index, range_reference_t<_Base>>>;
return static_cast<_ElemTy>(_STD get<_Index>(*_Current));
}
}

constexpr _Iterator& operator++() noexcept(noexcept(++_Current)) /* strengthened */ {
Expand Down Expand Up @@ -3835,7 +3844,12 @@ namespace ranges {
#if _ITERATOR_DEBUG_LEVEL != 0
_Verify_offset(_Idx);
#endif // _ITERATOR_DEBUG_LEVEL != 0
return _STD get<_Index>(*(_Current + _Idx));
if constexpr (is_reference_v<range_reference_t<_Base>>) {
return _STD get<_Index>(*(_Current + _Idx));
} else {
using _ElemTy = remove_cv_t<tuple_element_t<_Index, range_reference_t<_Base>>>;
return static_cast<_ElemTy>(_STD get<_Index>(*(_Current + _Idx)));
}
}

_NODISCARD friend constexpr bool operator==(const _Iterator& _Left, const _Iterator& _Right) noexcept(
Expand Down
8 changes: 8 additions & 0 deletions tests/std/tests/P0896R4_views_elements/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@ constexpr void instantiation_test() {
#endif // TEST_EVERYTHING
}

constexpr bool test_dangling() {
constexpr int input[] = {1, 5, 42, 1307};
const auto r = input | std::views::transform([](auto c) { return std::make_tuple(c, c); }) | std::views::keys;

assert(ranges::equal(input, r));
return true;
}

int main() {
{ // Validate copyable views
constexpr span<const P> s{some_pairs};
Expand Down

0 comments on commit a7965e5

Please sign in to comment.