Skip to content

Commit

Permalink
Merge pull request #1664 from YehezkelShB/FixPostIncrementOfCountedIt…
Browse files Browse the repository at this point in the history
…erator

Fix post increment of counted iterator
  • Loading branch information
ericniebler committed Feb 14, 2022
2 parents 95fd670 + 8907a67 commit 3e1ff2a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/range/v3/iterator/counted_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ namespace ranges
{
CPP_assert(std::is_void<decltype(current_++)>());
++current_;
--cnt_;
}
constexpr auto post_increment_(std::false_type) -> decltype(current_++)
{
Expand Down
1 change: 1 addition & 0 deletions test/iterator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ rv3_add_test(test.iter.iterator iter.iterator iterator.cpp)
rv3_add_test(test.iter.common_iterator iter.common_iterator common_iterator.cpp)
rv3_add_test(test.iter.reverse_iterator iter.reverse_iterator reverse_iterator.cpp)
rv3_add_test(test.iter.unformatted_ostream_iterator iter.unformatted_ostream_iterator unformatted_ostream_iterator.cpp)
rv3_add_test(test.iter.counted_iterator iter.counted_iterator counted_iterator.cpp)
40 changes: 40 additions & 0 deletions test/iterator/counted_iterator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Range v3 library
//
// Copyright Eric Niebler 2014-present
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3

#include <range/v3/iterator/counted_iterator.hpp>

#include "../simple_test.hpp"

// iterator that models input_iterator (has void-returning postfix increment operator)
struct Iterator
{
using value_type = int;
using difference_type = int;

int counter = 0;

int operator*() const { return counter; }
Iterator& operator++() { ++counter; return *this; }
void operator++(int) { ++counter; }
bool operator==(const Iterator& rhs) const { return counter == rhs.counter; }
bool operator!=(const Iterator& rhs) const { return !(*this == rhs); }
};

int main()
{
CPP_assert(ranges::input_iterator<Iterator>);
auto cnt = ranges::counted_iterator<Iterator>(Iterator(), 1);
CHECK(*cnt == 0);
cnt++;
CHECK(cnt == ranges::default_sentinel);

return test_result();
}

0 comments on commit 3e1ff2a

Please sign in to comment.