constexpr const I& base() const & noexcept; | (1) | (since C++20) |
constexpr I base() &&; | (2) | (since C++20) |
Returns the underlying base iterator.
(none).
May throw implementation-defined exceptions.
#include <algorithm> #include <iostream> #include <iterator> #include <type_traits> #include <vector> int main() { std::vector<int> v = { 0, 1, 2, 3, 4 }; std::reverse_iterator<std::vector<int>::iterator> reverse{v.rbegin()}; std::counted_iterator counted{reverse, 3}; static_assert(std::is_same<decltype(counted.base()), std::reverse_iterator<std::vector<int>::iterator>>{}); std::cout << "Print with reverse_iterator: "; for (auto r = counted.base(); r != v.rend(); ++r) std::cout << *r << ' '; std::cout << '\n'; std::cout << "Print with counted_iterator: "; for (; counted != std::default_sentinel; ++counted) std::cout << counted[0] << ' '; std::cout << '\n'; }
Output:
Print with reverse_iterator: 4 3 2 1 0 Print with counted_iterator: 4 3 2
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3391 | C++20 | the const version of base returns a copy of the underlying iterator | returns a reference |
LWG 3593 | C++20 | the const version of base returns a reference but might not be noexcept | made noexcept |
(C++20) | accesses the pointed-to element (public member function) |
(C++20) | returns the distance to the end (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/iterator/counted_iterator/base