std::counte...erator::base(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

std::counted_iterator::base - std::counted_iterator::base 🫢

okay so this is the std::counted_iterator::base(3) page, and the manual name folded in near the top is C++ Standard Libary (spelled exactly like that in the header, no cap, we love a typo moment πŸ’…).


🌸 Synopsis 🌸

constexpr const I& base() const& noexcept; (1) (since C++20)
constexpr I base() &&;                     (2) (since C++20)

Returns the underlying base iterator. That's the whole vibe πŸ¦‹

  1. Returns a reference to the underlying iterator.
  2. Move constructs the return value from the underlying iterator.

✨ Parameters ✨

none. zero. not a single parameter, bestie β€” (none) 🫢

πŸŽ€ Return value πŸŽ€

  1. A reference to the underlying iterator.
  2. An iterator move constructed from the underlying iterator.

πŸ”₯ Exceptions πŸ”₯

May throw implementation-defined exceptions. so like, it may throw, and the implementation gets to define them β€” noted πŸ‘‘

🌈 Example 🌈

// Run this code

#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> const&
    >{});

    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

🧚 Defect reports 🧚

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. periodt.

  • DR: LWG 3391 Β· Applied to: C++20 Β· Behavior as published: the const version of base returns a copy of the underlying iterator Β· Correct behavior: returns a reference
  • DR: LWG 3593 Β· Applied to: C++20 Β· Behavior as published: the const version of base returns a reference but might not be noexcept Β· Correct behavior: made noexcept

πŸ‘‘ See also πŸ‘‘

  • operator* β€” accesses the pointed-to element
  • operator-> β€” (public member function) (C++20)
  • count β€” returns the distance to the end (C++20) (public member function)

sign-off cuteness: source is http://cppreference.com, date is 2024.06.10, and the footer page is std::counte...erator::base(3) β€” mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from