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

<algorithm>: fix GH-1932 #1933

Merged
merged 2 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions stl/inc/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -4072,9 +4072,12 @@ namespace ranges {

// VARIABLE ranges::unique_copy
// clang-format off
template <class _It, class _Ty>
concept _Is_input_with_value_type = input_iterator<_It> && same_as<iter_value_t<_It>, _Ty>;
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved

template <class _It, class _Out>
concept _Can_reread_or_store = forward_iterator<_It>
|| (input_iterator<_Out> && same_as<iter_value_t<_It>, iter_value_t<_Out>>)
|| _Is_input_with_value_type<_Out, iter_value_t<_It>>
|| indirectly_copyable_storable<_It, _Out>;
// clang-format on
class _Unique_copy_fn : private _Not_quite_object {
Expand Down Expand Up @@ -4127,7 +4130,7 @@ namespace ranges {
return {_STD move(_First), _STD move(_Result)};
}

if constexpr (input_iterator<_Out> && same_as<iter_value_t<_It>, iter_value_t<_Out>>) {
if constexpr (_Is_input_with_value_type<_Out, iter_value_t<_It>>) {
// Can reread _Result
*_Result = *_First;

Expand Down
13 changes: 13 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_unique_copy/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cassert>
#include <concepts>
#include <ranges>
#include <sstream>
#include <utility>

#include <range_algorithm_support.hpp>
Expand Down Expand Up @@ -142,8 +143,20 @@ constexpr bool run_tests() {
return true;
}

void test_gh1932() {
// Defend against regression of GH-1932, in which ranges::unique_copy instantiated
// iter_value_t<I> for a non-input iterator I.

istringstream str("42 42 42");
ostringstream result;
ranges::unique_copy(istream_iterator<int>{str}, istream_iterator<int>{}, ostream_iterator<int>{result, " "});
assert(result.str() == "42 ");
}

int main() {
STATIC_ASSERT(run_tests());
run_tests();

test_gh1932();
}
#endif // TEST_EVERYTHING