From cppreference.com
constexprcommon_iterator()requiresstd::default_initializable<I>=default; (1) (since C++20)constexprcommon_iterator(Ii); (2) (since C++20)constexprcommon_iterator(Ss); (3) (since C++20)template<classI2,classS2>requiresstd::convertible_to<constI2&,I>&&std::convertible_to<constS2&,S>constexprcommon_iterator(constcommon_iterator<I2,S2>&x); (4) (since C++20)Constructs a new iterator adaptor, effectively initializes the underlying std::variant<I,S> member object var to hold an I (iterator) or S (sentinel) object.
1) Default constructor. Default-initializes var. After construction, var holds a value-initialized I object.
Operations on the resulting iterator adaptor have defined behavior if and only if the corresponding operations on a value-initialized I also have defined behavior.
2) After construction, var holds an I object move-constructed from i.
3) After construction, var holds an S object move-constructed from s.
4) After construction, var holds an I or S object initialized from the I2 or S2 held by x.var, if x.var holds that alternative, respectively.
If x.var.valueless_by_exception() is true, the behavior is undefined.
(until C++26)If x.var.valueless_by_exception() is true:
If the implementation is
, a
occurs.
If the implementation is not hardened, the behavior is undefined.
(since C++26)Parameters
i - iterator to adapt s - sentinel to adapt x - iterator adaptor to copy Example
Run this code
#include<algorithm>#include<iostream>#include<iterator>#include<numeric>#include<vector>intmain(){std::vectorv{3,1,4,1,5,9,2};template<typenameIter>usingCommonIter=std::common_iterator<std::counted_iterator<Iter>,std::default_sentinel_t>;usingCI=CommonIter<std::vector<int>::iterator>;CIunused;// (1)CIstart{std::counted_iterator{std::next(begin(v)),ssize(v)-2}};// (2)CIfinish{std::default_sentinel};// (3)CIfirst{start};// (4)CIlast{finish};// (4)std::copy(first,last,std::ostream_iterator<int>{std::cout," "});std::cout<<'\n';CommonIter<std::ostream_iterator<double>>beg{std::counted_iterator{std::ostream_iterator<double>{std::cout,"; "},5}},end{std::default_sentinel};std::iota(beg,end,3.1);std::cout<<'\n';}Output:
1 4 1 5 9 3.1; 4.1; 5.1; 6.1; 7.1; See also