# std::iterator_traits<std::common_iterator>

> C++ Atlas: `cppatlas.com/w/cpp/iterator/common_iterator/iterator_traits` | Source: https://en.cppreference.com/w/cpp/iterator/common_iterator/iterator_traits | Domain: Iterator | Standards: C++20

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

## Official Specification Summary (cppreference)

```cpp
// 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`: - `std::forward_iterator_tag`, if `I` models `std::forward_iterator`. - `std::input_iterator_tag` otherwise.
- `iterator_category` (conditionally present): - Not defined, if `std::iter_difference_t<I>` is not an integral type. - `std::forward_iterator_tag` otherwise, if `std::iterator_traits<I>::iterator_category` is valid and denotes a type that models `std::derived_from<std::forward_iterator_tag>`. - `std::input_iterator_tag` otherwise.
- `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

```cpp
#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

- [`iterator_traits`](https://en.cppreference.com/w/cpp/iterator/iterator_traits): provides uniform interface to the properties of an iterator (class template)
- [`std::incrementable_traits<std::common_iterator>`](https://en.cppreference.com/w/cpp/iterator/common_iterator/incrementable_traits) (C++20): computes the associated difference type of the `std::common_iterator` type (class template specialization)

---
Attribution: content from https://en.cppreference.com/w/cpp/iterator/common_iterator/iterator_traits (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.