Skip to content

Commit

Permalink
<algorithm>: fix GH-1932 (#1933)
Browse files Browse the repository at this point in the history
* <algorithm>: fix GH-1932

... by extracting the constant expression we'd like to short-circuit into a concept.

Fixes #1932.
  • Loading branch information
CaseyCarter committed Jun 4, 2021
1 parent 20288f9 commit 4c862ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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>;

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

0 comments on commit 4c862ee

Please sign in to comment.