std::iterator_traits<std::common_iterator>

From cppreference.com
 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
 
Defined in header <iterator>
template< std::input_iterator I, class S >
struct iterator_traits<std::common_iterator<I, S>>;
(since C++20)

Provides the uniform interface to the properties of the std::common_iterator type.

Member types

Type name Definition
iterator_concept
iterator_category
(conditionally present)
value_type std::iter_value_t<I>
difference_type std::iter_difference_t<I>
pointer
  • decltype(c.operator->()), if it is well-formed (where c is an lvalue of type const std::common_iterator<I, S>).
  • void otherwise.
reference std::iter_reference_t<I>

Example

#include <iterator>
#include <type_traits>
#include <vector>
 
int main()
{
    std::vector v{1, 2, 3, 4};
    using CV = std::common_iterator<
                    std::counted_iterator<std::vector<int>::iterator>,
                    std::default_sentinel_t>;
    CV i{std::counted_iterator{v.begin(), 3}};
    using TRCV = std::iterator_traits<decltype(i)>;
    static_assert(std::is_same<TRCV::iterator_concept, std::forward_iterator_tag>());
    static_assert(std::is_same<TRCV::iterator_category, std::forward_iterator_tag>());
    static_assert(std::is_same<TRCV::value_type, int>());
    static_assert(std::is_same<TRCV::difference_type,
                  std::vector<int>::difference_type>());
    static_assert(std::is_same<TRCV::pointer, decltype(i.operator->())>());
    static_assert(std::is_same<TRCV::reference, int&>());
 
    CV s{std::default_sentinel};
    using TRCS = std::iterator_traits<decltype(s)>;
    static_assert(std::is_same<TRCS::iterator_concept, std::forward_iterator_tag>());
    static_assert(std::is_same<TRCS::iterator_category, std::forward_iterator_tag>());
    static_assert(std::is_same<TRCS::value_type, int>());
    static_assert(std::is_same<TRCS::difference_type,
                  std::vector<int>::difference_type>());
    static_assert(std::is_same<TRCS::pointer, decltype(s.operator->())>());
    static_assert(std::is_same<TRCS::reference, int&>());
}

Defect reports

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

DR Applied to Behavior as published Correct behavior
LWG 3749 C++20 if std::iter_difference_t<I> is not an integer type,
iterator_category was std::input_iterator_tag
iterator_category is
undefined in this case

See also

provides uniform interface to the properties of an iterator
(class template)
computes the associated difference type of the std::common_iterator type
(class template specialization)