< cpp‎ | iterator
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
Iterator adaptors
Stream iterators
Iterator customization points
Iterator operations
Range access
Defined in header <iterator>
template< std::input_or_output_iterator I, std::sentinel_for<I> S >

  requires ( !std::same_as<I, S> && std::copyable<I> )

    class common_iterator;
(since C++20)

std::common_iterator is an iterator I / sentinel S adaptor that may represent a non-common range (where the types of I and S differ) as a common_range, by containing either an iterator or a sentinel, and defining the appropriate equality comparison operators operator==.
std::common_iterator can be used as a "bridge" between sequences represented by iterator/sentinel pair and legacy functions that expect common_range-like sequences.


[edit] Member functions

constructs a new iterator adaptor
(public member function) [edit]
assigns another iterator adaptor
(public member function) [edit]
accesses the pointed-to element
(public member function) [edit]
advances the iterator adaptor
(public member function) [edit]

[edit] Member objects

Member name Definition
var (private) an object of type std::variant<I, S>, the name is for exposition only

[edit] Non-member functions

compares the underlying iterators or sentinels
(function template) [edit]
computes the distance between two iterator adaptors
(function template) [edit]
casts the result of dereferencing the underlying iterator to its associated rvalue reference type
(function) [edit]
swaps the objects pointed to by two underlying iterators
(function template) [edit]

[edit] Helper classes

computes the associated difference type of the std::common_iterator type
(class template specialization) [edit]
provides uniform interface to the properties of the std::common_iterator type
(class template specialization) [edit]

[edit] Example

#include <algorithm>
#include <list>
#include <iostream>
#include <iterator>
#include <string>
template <class ForwardIter>
void fire(ForwardIter first, ForwardIter last) {
    std::copy(first, last, std::ostream_iterator<std::string>{std::cout, " "});    
int main() {
    std::list<std::string> stars{"Pollux", "Arcturus", "Mira", "Aldebaran", "Sun"};
    using IT = std::common_iterator<
    fire( IT(std::counted_iterator(stars.begin(), stars.size()-1)),
          IT(std::default_sentinel) );


Pollux Arcturus Mira Aldebaran

[edit] References

  • C++23 standard (ISO/IEC 14882:2023):
  • 23.5.5 Common iterators [iterators.common]
  • C++20 standard (ISO/IEC 14882:2020):
  • 23.5.4 Common iterators [iterators.common]

[edit] See also

specifies that a range has identical iterator and sentinel types
(concept) [edit]
converts a view into a common_range
(class template) (range adaptor object) [edit]