Skip to content

Commit

Permalink
Fix counted_iterator unwrapping bug (#1213)
Browse files Browse the repository at this point in the history
  • Loading branch information
CaseyCarter authored Aug 22, 2020
1 parent 22d061b commit 8ee6e65
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions stl/inc/iterator
Original file line number Diff line number Diff line change
Expand Up @@ -770,11 +770,11 @@ public:

_NODISCARD constexpr counted_iterator<_Unwrapped_t<const _Iter&>>
_Unwrapped() const& requires _Unwrappable_v<const _Iter&> {
return static_cast<counted_iterator<_Unwrapped_t<const _Iter&>>>(_Current._Unwrapped());
return counted_iterator<_Unwrapped_t<const _Iter&>>{_Current._Unwrapped(), _Length};
}

_NODISCARD constexpr counted_iterator<_Unwrapped_t<_Iter>> _Unwrapped() && requires _Unwrappable_v<const _Iter&> {
return static_cast<counted_iterator<_Unwrapped_t<_Iter>>>(_STD move(_Current)._Unwrapped());
_NODISCARD constexpr counted_iterator<_Unwrapped_t<_Iter>> _Unwrapped() && requires _Unwrappable_v<_Iter> {
return counted_iterator<_Unwrapped_t<_Iter>>{_STD move(_Current)._Unwrapped(), _Length};
}

static constexpr bool _Unwrap_when_unverified = _Do_unwrap_when_unverified_v<_Iter>;
Expand Down
10 changes: 10 additions & 0 deletions tests/std/tests/P0896R4_counted_iterator/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cassert>
#include <concepts>
#include <iterator>
#include <list>
#include <memory>
#include <type_traits>

Expand Down Expand Up @@ -304,4 +305,13 @@ struct instantiator {
int main() {
STATIC_ASSERT((with_writable_iterators<instantiator, int>::call(), true));
with_writable_iterators<instantiator, int>::call();

{ // Validate unwrapping
list<int> lst{0, 1, 2};
counted_iterator ci{lst.begin(), 2};
same_as<counted_iterator<_Unwrapped_t<list<int>::iterator>>> auto uci = _Get_unwrapped(ci);
++uci;
_Seek_wrapped(ci, uci);
assert((ci == counted_iterator{ranges::next(lst.begin()), 1}));
}
}

0 comments on commit 8ee6e65

Please sign in to comment.