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

Implement LWG-3520 #2200

Merged
merged 7 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 0 additions & 9 deletions stl/inc/ranges
Original file line number Diff line number Diff line change
Expand Up @@ -1988,15 +1988,6 @@ namespace ranges {
return *_It;
}
}

friend constexpr void iter_swap(const _Iterator& _Left, const _Iterator& _Right) _NOEXCEPT_IDL0(noexcept(
_RANGES iter_swap(_Left._Current, _Right._Current))) requires indirectly_swappable<iterator_t<_Base>> {
#if _ITERATOR_DEBUG_LEVEL != 0
_Left._Check_dereference();
_Right._Check_dereference();
#endif // _ITERATOR_DEBUG_LEVEL != 0
_RANGES iter_swap(_Left._Current, _Right._Current);
}
};

template <bool _Const>
Expand Down
5 changes: 5 additions & 0 deletions tests/std/include/range_algorithm_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1449,3 +1449,8 @@ template <class R>
concept CanBool = requires(R&& r) {
std::forward<R>(r) ? true : false;
};

template <class I>
concept CanIterSwap = requires(I&& i1, I&& i2) {
ranges::iter_swap(std::forward<I>(i1), std::forward<I>(i2));
};
26 changes: 16 additions & 10 deletions tests/std/tests/P0896R4_views_transform/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,16 +509,7 @@ struct iterator_instantiator {
assert(ranges::iter_move(i0) == add8(mutable_ints[0])); // NB: moving from int leaves it unchanged
STATIC_ASSERT(NOEXCEPT_IDL0(ranges::iter_move(i0)));

if constexpr (forward_iterator<Iter>) {
auto i1 = ranges::next(i0);
ranges::iter_swap(i0, i1);
assert(mutable_ints[0] == 1);
assert(mutable_ints[1] == 0);
ranges::iter_swap(i1, i0);
assert(mutable_ints[0] == 0);
assert(mutable_ints[1] == 1);
STATIC_ASSERT(NOEXCEPT_IDL0(ranges::iter_swap(i0, i1)));
}
STATIC_ASSERT(!CanIterSwap<decltype(i0)>);
}

{ // Validate increments
Expand Down Expand Up @@ -784,4 +775,19 @@ int main() {

(void) views::transform(Fn{})(span<int>{});
}

{ // Validate that iter_swap works when result of transformation is a lvalue reference
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
char base[] = "hello";
auto v = ranges::transform_view{base, [](char& c) -> char& { return c; }};
auto i1 = v.begin();
auto i2 = v.begin() + 1;

assert(*i1 == 'h');
assert(*i2 == 'e');

ranges::iter_swap(i1, i2);

assert(*i1 == 'e');
assert(*i2 == 'h');
}
}
23 changes: 0 additions & 23 deletions tests/std/tests/P0896R4_views_transform_death/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,26 +287,6 @@ void test_iter_move_value_initialized_iterator() {
(void) ranges::iter_move(i); // cannot dereference value-initialized transform_view iterator
}

void test_iter_swap_value_initialized_iterators() {
ranges::iterator_t<TV> i0{};
ranges::iterator_t<TV> i1{};
(void) ranges::iter_swap(i0, i1); // cannot dereference value-initialized transform_view iterator
}

void test_iter_swap_value_initialized_iterator_left() {
ranges::iterator_t<TV> i0{};
TV r{some_ints, lambda};
ranges::iterator_t<TV> i1 = r.begin();
(void) ranges::iter_swap(i0, i1); // cannot dereference value-initialized transform_view iterator
}

void test_iter_swap_value_initialized_iterator_right() {
TV r{some_ints, lambda};
ranges::iterator_t<TV> i0 = r.begin();
ranges::iterator_t<TV> i1{};
(void) ranges::iter_swap(i0, i1); // cannot dereference value-initialized transform_view iterator
}

void test_sentinel_compare_value_initialized() {
auto r = ranges::subrange{counted_iterator{some_ints, ranges::distance(some_ints)}, default_sentinel}
| views::transform(lambda);
Expand Down Expand Up @@ -382,9 +362,6 @@ int main(int argc, char* argv[]) {
test_operator_minus_incompatible_different,
test_operator_minus_incompatible_value_initialized,
test_iter_move_value_initialized_iterator,
test_iter_swap_value_initialized_iterators,
test_iter_swap_value_initialized_iterator_left,
test_iter_swap_value_initialized_iterator_right,
test_sentinel_compare_value_initialized,
test_sentinel_difference_value_initialized,
test_flipped_sentinel_difference_value_initialized,
Expand Down