# Iterator library

> C++ Atlas: `cppatlas.com/w/cpp/iterator` | Source: https://en.cppreference.com/w/cpp/iterator | Domain: Iterator | Standards: C++11, C++14, C++17, C++20, C++23, C++26

Iterators are a generalization of pointers that allow a C++ program to work with different data structures (for example, containers and ranges(since C++20)) in a uniform manner.

## Executive Mental Model (Study Companion)

**Core Intuition:** Only named concepts participate in atomic constraint subsumption—writing identical raw requires expressions twice produces two distinct atoms and an ambiguous overload!

- **Standard Era:** C++17 → C++20 → C++23
- **Primary ISO Clauses:** [temp.constr], [temp.constr.order], [expr.prim.req], [temp.res]
- **CppCon Talk Angle:** Visualize overload selection as a DAG lattice: std::contiguous_iterator subsumes random_access_iterator, which subsumes bidirectional_iterator, letting std::ranges pick the fastest algorithm cleanly.
- **Common Audience Trap:** Writing requires requires(...) — the first keyword introduces a constraint clause, the second starts a boolean requires-expression.
- **Stage Takeaway:** If two constrained overloads ever tie with 'ambiguous call', check whether you wrote an ad-hoc boolean expression instead of composing named concepts.

## Memory Map Walkthrough: C++20 Concept Subsumption Lattice & Overload Resolver

C++20 concepts normalize constraints into conjunctions and disjunctions of atomic constraints. Subsumption ([temp.constr.order]) compares two constrained overloads: if constraint P logically implies constraint Q strictly because every atomic constraint in Q appears inside P from the exact same named concept definition, then P subsumes Q and wins overload resolution without ambiguity. Writing raw boolean requires clauses inline—like requires sizeof(T) > 4 && std::integral<T> vs requires std::integral<T>—works only for the shared named concept; repeating raw expressions creates separate atomic identities that never subsume each other. Meanwhile, requires-expressions check syntactic validity and return-type constraints at compile time without ever evaluating runtime operands.

### Named concepts & atomic constraint identity ([temp.constr.atomic] & [temp.constr.order])

During overload resolution, the compiler converts constrained templates into Disjunctive/Conjunctive Normal Form of atomic constraints—each tied to its exact source-code token position and parameter mapping.

If overload A writes template<class T> requires (std::integral<T> && sizeof(T) >= 4) and overload B writes template<class T> requires (std::integral<T> && sizeof(T) >= 4 && alignof(T) >= 4), the sizeof(T) >= 4 expression in A and B came from TWO separate source locations, so neither subsumes the other! Wrapping sizeof(T) >= 4 in a named concept Wide<T> shares one atomic identity.

*Subsumption works ONLY through shared named concepts:*
```cpp
template <class T>
concept SignedInt = std::integral<T> && std::is_signed_v<T>;

void dispatch(std::integral auto x); // Q: atomic(std::integral)
void dispatch(SignedInt auto x);     // P: atomic(std::integral) && atomic(is_signed)

dispatch(42); // P subsumes Q -> unambiguously calls SignedInt overload!
```

**Key takeaway:** Always factor reusable predicates into named concept declarations if you want overloads to partially order by specificity.

### The requires requires idiom decoded ([temp.constr.decl] & [expr.prim.req])

The first requires introduces a requires-clause (expecting a constant boolean expression). The second requires opens a requires-expression—an unevaluated compile-time check returning true if every nested expression compiles.

For real libraries, prefer defining a named concept above the function rather than embedding anonymous requires requires directly on every signature.

*Clean named concept vs. inline requires-requires:*
```cpp
template <class R>
concept Reservable = requires(R& r, std::size_t n) {
    r.reserve(n);       // simple requirement: must compile (not evaluated)
    { r.capacity() } -> std::convertible_to<std::size_t>; // compound requirement
};
```

**Key takeaway:** Inside a requires-expression, expressions are unevaluated operands—side effects never run, and compound requirements validate return type concepts.

## Official Specification Summary (cppreference)

Iterators are a generalization of pointers that allow a C++ program to work with different data structures (for example, containers and ranges (since C++20)) in a uniform manner. The iterator library provides definitions for iterators, as well as iterator traits, adaptors, and utility functions.

Since iterators are an abstraction of pointers, their semantics are a generalization of most of the semantics of pointers in C++. This ensures that every function template that takes iterators works as well with regular pointers.

### Iterator categories

There are five (until C++17) six (since C++17) kinds of iterators: *LegacyInputIterator*, *LegacyOutputIterator*, *LegacyForwardIterator*, *LegacyBidirectionalIterator*, *LegacyRandomAccessIterator*, and *LegacyContiguousIterator* (since C++17). (See also *LegacyIterator* for the most basic kind of iterator.)

Instead of being defined by specific types, each category of iterator is defined by the operations that can be performed on it. This definition means that any type that supports the necessary operations can be used as an iterator -- for example, a pointer supports all of the operations required by *LegacyRandomAccessIterator*, so a pointer can be used anywhere a *LegacyRandomAccessIterator* is expected.

All of the iterator categories (except *LegacyOutputIterator*) can be organized into a hierarchy, where more powerful iterator categories (e.g. *LegacyRandomAccessIterator*) support the operations of less powerful categories (e.g. *LegacyInputIterator*). If an iterator falls into one of these categories and also satisfies the requirements of *LegacyOutputIterator*, then it is called a *mutable* iterator and supports *both* input and output. Non-mutable iterators are called *constant* iterators.

Iterators are called *constexpr* iterators if all operations provided to meet iterator category requirements are constexpr functions. (since C++20)

| Iterator category | Operations and storage requirement |   |   |   |   |   |   |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Iterator category | write | read | increment |   | decrement | random access | contiguous storage |
| Iterator category | write | read | without multiple passes | with multiple passes | decrement | random access | contiguous storage |
| *LegacyIterator* |   |   | Required |   |   |   |   |
| *LegacyOutputIterator* | Required |   | Required |   |   |   |   |
| *LegacyInputIterator* (mutable if supports write operation) |   | Required | Required |   |   |   |   |
| *LegacyForwardIterator* (also satisfies *LegacyInputIterator*) |   | Required | Required | Required |   |   |   |
| *LegacyBidirectionalIterator* (also satisfies *LegacyForwardIterator*) |   | Required | Required | Required | Required |   |   |
| *LegacyRandomAccessIterator* (also satisfies *LegacyBidirectionalIterator*) |   | Required | Required | Required | Required | Required |   |
| *LegacyContiguousIterator*[1] (also satisfies *LegacyRandomAccessIterator*) |   | Required | Required | Required | Required | Required | Required |

1. ↑ *LegacyContiguousIterator* category was only formally specified in C++17, but the iterators of `std::vector`, `std::basic_string`, `std::array`, and `std::valarray`, as well as pointers into C arrays are often treated as a separate category in pre-C++17 code.

Note: A type supporting the required operations in a row of the table above does not necessarily fall into the corresponding category, see the category page for the complete list of requirements.

### Definitions

#### Types and writability

An input iterator `i` supports the expression `*i`, resulting in a value of some object type `T`, called the *value type* of the iterator.

An output iterator `i` has a non-empty set of types that are *writable* (until C++20) `indirectly_writable` (since C++20) to the iterator; for each such type `T`, the expression `*i = o` is valid where `o` is a value of type `T`.

For every iterator type `X` for which equality is defined (until C++20), there is a corresponding signed integer (until C++20) integer-like (since C++20) type called the *difference type* of the iterator.

#### Dereferenceability and validity

Just as a regular pointer to an array guarantees that there is a pointer value pointing past the last element of the array, so for any iterator type there is an iterator value that points past the last element of a corresponding sequence. Such a value is called a *past-the-end* value.

Values of an iterator `i` for which the expression `*i` is defined are called *dereferenceable*. The standard library never assumes that past-the-end values are dereferenceable.

Iterators can also have *singular* values that are not associated with any sequence. Results of most expressions are undefined for singular values; the only exceptions are

- the assignment of a non-singular value to an iterator that holds a singular value,
- destroying an iterator that holds a singular value, and,
- for iterators that meet the *DefaultConstructible* requirements, using a value-initialized iterator as the source of a copy or move (since C++11) operation.

In these cases the singular value is overwritten the same way as any other value. Dereferenceable values are always non-singular.

An *invalid* iterator is an iterator that may be singular.

#### Ranges

Most of the standard library’s algorithmic templates that operate on data structures have interfaces that use ranges.

(until C++20):

An iterator `j` is called *reachable* from an iterator `i` if and only if there is a finite sequence of applications of the expression `++i` that makes `i == j`. If `j` is reachable from `i`, they refer to elements of the same sequence.

A *range* is a pair of iterators that designate the beginning and end of the computation. A range `[i,` `i)` is an empty range; in general, a range `[i,` `j)` refers to the elements in the data structure starting with the element pointed to by `i` and up to but not including the element pointed to by `j`.

Range `[i,` `j)` is *valid* if and only if `j` is reachable from `i`.

(since C++20):

A *range* can be denoted by either

- `[i,` `s)`, with an iterator `i` and a *sentinel* `s` that designate the beginning and end of the computation (`i` and `s` can have different types), or
- `i` `+` `[​0​,` `n)`, with an iterator `i` and a count `n` that designate the beginning and the number of elements to which the computation is to be applied.

##### Iterator-sentinel range

An iterator and a sentinel denoting a range are comparable. `[i,` `s)` is empty if `i == s`; otherwise, `[i,` `s)` refers to the elements in the data structure starting with the element pointed to by `i` and up to but not including the element, if any, pointed to by the first iterator `j` such that `j == s`.

A sentinel `s` is called *reachable* from an iterator `i` if and only if there is a finite sequence of applications of the expression `++i` that makes `i == s`.

If `s` is reachable from `i`, `[i,` `s)` denotes a *valid* range.

##### Counted range

A *counted range* `i` `+` `[​0​,` `n)` is empty if `n == 0`; otherwise, `i` `+` `[​0​,` `n)` refers to the `n` elements in the data structure starting with the element pointed to by `i` and up to but not including the element, if any, pointed to by the result of `n` applications of `++i`.

A counted range `i` `+` `[​0​,` `n)` is *valid* if and only if

- `n == 0`; or
- all of the following conditions are satisfied:
  - `n` is positive,
  - `i` is dereferenceable, and
  - `++i` `+` `[​0​,` `--n)` is valid.

The result of the application of functions in the standard library to invalid ranges is undefined.

### Iterator concepts (since C++20)

A new system of iterators based on concepts that are different from C++17 iterators. While the basic taxonomy remains similar, the requirements for individual iterator categories are somewhat different.

Defined in namespace `std`

- `indirectly_readable` (C++20): specifies that a type is indirectly readable by applying operator `*` (concept)
- `indirectly_writable` (C++20): specifies that a value can be written to an iterator's referenced object (concept)
- `weakly_incrementable` (C++20): specifies that a `semiregular` type can be incremented with pre- and post-increment operators (concept)
- `incrementable` (C++20): specifies that the increment operation on a `weakly_incrementable` type is equality-preserving and that the type is `equality_comparable` (concept)
- `is-integer-like` (C++20), `is-signed-integer-like` (C++20): specifies that a type behaves as a (signed) integer type (exposition-only concept*)
- `input_or_output_iterator` (C++20): specifies that objects of a type can be incremented and dereferenced (concept)
- `sentinel_for` (C++20): specifies a type is a sentinel for an `input_or_output_iterator` type (concept)
- `sized_sentinel_for` (C++20): specifies that the `-` operator can be applied to an iterator and a sentinel to calculate their difference in constant time (concept)
- `input_iterator` (C++20): specifies that a type is an input iterator, that is, its referenced values can be read and it can be both pre- and post-incremented (concept)
- `output_iterator` (C++20): specifies that a type is an output iterator for a given value type, that is, values of that type can be written to it and it can be both pre- and post-incremented (concept)
- `forward_iterator` (C++20): specifies that an `input_iterator` is a forward iterator, supporting equality comparison and multi-pass (concept)
- `bidirectional_iterator` (C++20): specifies that a `forward_iterator` is a bidirectional iterator, supporting movement backwards (concept)
- `random_access_iterator` (C++20): specifies that a `bidirectional_iterator` is a random-access iterator, supporting advancement in constant time and subscripting (concept)
- `contiguous_iterator` (C++20): specifies that a `random_access_iterator` is a contiguous iterator, referring to elements that are contiguous in memory (concept)

### Iterator associated types (since C++20)

Defined in namespace `std`

- `incrementable_traits` (C++20): computes the difference type of a `weakly_incrementable` type (class template)
- `indirectly_readable_traits` (C++20): computes the value type of an `indirectly_readable` type (class template)
- `iter_value_t` (C++20), `iter_reference_t` (C++20), `iter_const_reference_t` (C++23), `iter_difference_t` (C++20), `iter_rvalue_reference_t` (C++20), `iter_common_reference_t` (C++20): computes the associated types of an iterator (alias template)

### Iterator primitives

- `iterator_traits`: provides uniform interface to the properties of an iterator (class template)
- `input_iterator_tag`, `output_iterator_tag`, `forward_iterator_tag`, `bidirectional_iterator_tag`, `random_access_iterator_tag`, `contiguous_iterator_tag` (C++20): empty class types used to indicate iterator categories (class)
- `iterator` (deprecated in C++17): base class to ease the definition of required types for simple iterators (class template)

### Iterator customization points (since C++20)

Defined in namespace `std::ranges`

- `iter_move` (C++20): casts the result of dereferencing an object to its associated rvalue reference type (customization point object)
- `iter_swap` (C++20): swaps the values referenced by two dereferenceable objects (customization point object)

### Algorithm concepts and utilities (since C++20)

A set of concepts and related utility templates designed to ease constraining common algorithm operations.

Defined in header `<iterator>`

Defined in namespace `std`

**Indirect callable concepts**

- `indirectly_unary_invocable` (C++20), `indirectly_regular_unary_invocable` (C++20): specifies that a callable type can be invoked with the result of dereferencing an `indirectly_readable` type (concept)
- `indirect_unary_predicate` (C++20): specifies that a callable type, when invoked with the result of dereferencing an `indirectly_readable` type, satisfies `predicate` (concept)
- `indirect_binary_predicate` (C++20): specifies that a callable type, when invoked with the result of dereferencing two `indirectly_readable` types, satisfies `predicate` (concept)
- `indirect_equivalence_relation` (C++20): specifies that a callable type, when invoked with the result of dereferencing two `indirectly_readable` types, satisfies `equivalence_relation` (concept)
- `indirect_strict_weak_order` (C++20): specifies that a callable type, when invoked with the result of dereferencing two `indirectly_readable` types, satisfies `strict_weak_order` (concept)

**Common algorithm requirements**

- `indirectly_movable` (C++20): specifies that values may be moved from an `indirectly_readable` type to an `indirectly_writable` type (concept)
- `indirectly_movable_storable` (C++20): specifies that values may be moved from an `indirectly_readable` type to an `indirectly_writable` type and that the move may be performed via an intermediate object (concept)
- `indirectly_copyable` (C++20): specifies that values may be copied from an `indirectly_readable` type to an `indirectly_writable` type (concept)
- `indirectly_copyable_storable` (C++20): specifies that values may be copied from an `indirectly_readable` type to an `indirectly_writable` type and that the copy may be performed via an intermediate object (concept)
- `indirectly_swappable` (C++20): specifies that the values referenced by two `indirectly_readable` types can be swapped (concept)
- `indirectly_comparable` (C++20): specifies that the values referenced by two `indirectly_readable` types can be compared (concept)
- `permutable` (C++20): specifies the common requirements of algorithms that reorder elements in place (concept)
- `mergeable` (C++20): specifies the requirements of algorithms that merge sorted sequences into an output sequence by copying elements (concept)
- `sortable` (C++20): specifies the common requirements of algorithms that permute sequences into ordered sequences (concept)

**Utilities**

- `indirect_result_t` (C++20): computes the result of invoking a callable object on the result of dereferencing some set of `indirectly_readable` types (alias template)
- `projected` (C++20): helper template for specifying the constraints on algorithms that accept projections (class template)
- `projected_value_t` (C++26): computes the value type of an `indirectly_readable` type by projection (alias template)

### Iterator adaptors

- `reverse_iterator`: iterator adaptor for reverse-order traversal (class template)
- `make_reverse_iterator` (C++14): creates a `std::reverse_iterator` of type inferred from the argument (function template)
- `back_insert_iterator`: iterator adaptor for insertion at the end of a container (class template)
- `back_inserter`: creates a `std::back_insert_iterator` of type inferred from the argument (function template)
- `front_insert_iterator`: iterator adaptor for insertion at the front of a container (class template)
- `front_inserter`: creates a `std::front_insert_iterator` of type inferred from the argument (function template)
- `insert_iterator`: iterator adaptor for insertion into a container (class template)
- `inserter`: creates a `std::insert_iterator` of type inferred from the argument (function template)
- `basic_const_iterator` (C++23): iterator adaptor that converts an iterator into a constant iterator (class template)
- `const_iterator` (C++23): computes a constant iterator type for a given type (alias template)
- `const_sentinel` (C++23): computes a sentinel type to be used with constant iterators (alias template)
- `make_const_iterator` (C++23): creates a `std::const_iterator` of type inferred from the argument (function template)
- `make_const_sentinel` (C++23): creates a `std::const_sentinel` of type inferred from the argument (function template)
- `move_iterator` (C++11): iterator adaptor which dereferences to an rvalue (class template)
- `move_sentinel` (C++20): sentinel adaptor for `std::move_iterator` (class template)
- `make_move_iterator` (C++11): creates a `std::move_iterator` of type inferred from the argument (function template)
- `common_iterator` (C++20): adapts an iterator type and its sentinel into a common iterator type (class template)
- `default_sentinel_t` (C++20): default sentinel for use with iterators that know the bound of their range (class)
- `counted_iterator` (C++20): iterator adaptor that tracks the distance to the end of the range (class template)
- `unreachable_sentinel_t` (C++20): sentinel that always compares unequal to any `weakly_incrementable` type (class)

### Stream iterators

- `istream_iterator`: input iterator that reads from `std::basic_istream` (class template)
- `ostream_iterator`: output iterator that writes to `std::basic_ostream` (class template)
- `istreambuf_iterator`: input iterator that reads from `std::basic_streambuf` (class template)
- `ostreambuf_iterator`: output iterator that writes to `std::basic_streambuf` (class template)

### Iterator operations

Defined in header `<iterator>`

- `advance`: advances an iterator by given distance (function template)
- `distance`: returns the distance between two iterators (function template)
- `next` (C++11): increment an iterator (function template)
- `prev` (C++11): decrement an iterator (function template)
- `ranges::advance` (C++20): advances an iterator by given distance or to a given bound (algorithm function object)
- `ranges::distance` (C++20): returns the distance between an iterator and a sentinel, or between the beginning and end of a range (algorithm function object)
- `ranges::next` (C++20): increment an iterator by a given distance or to a bound (algorithm function object)
- `ranges::prev` (C++20): decrement an iterator by a given distance or to a bound (algorithm function object)

### Range access (since C++11)

These non-member function templates provide a generic interface for containers, plain arrays, and `std::initializer_list`.

Defined in header `<array>`

Defined in header `<deque>`

Defined in header `<flat_map>`

Defined in header `<flat_set>`

Defined in header `<forward_list>`

Defined in header `<inplace_vector>`

Defined in header `<iterator>`

Defined in header `<list>`

Defined in header `<map>`

Defined in header `<regex>`

Defined in header `<set>`

Defined in header `<span>`

Defined in header `<string>`

Defined in header `<string_view>`

Defined in header `<unordered_map>`

Defined in header `<unordered_set>`

Defined in header `<vector>`

Defined in namespace `std`

- `begin` (C++11), `cbegin` (C++14): returns an iterator to the beginning of a container or array (function template)
- `end` (C++11), `cend` (C++14): returns an iterator to the end of a container or array (function template)
- `rbegin` (C++14), `crbegin`: returns a reverse iterator to the beginning of a container or array (function template)
- `rend` (C++14), `crend`: returns a reverse end iterator for a container or array (function template)
- `size` (C++17), `ssize` (C++20): returns the size of a container or array (function template)
- `empty` (C++17): checks whether the container is empty (function template)
- `data` (C++17): obtains the pointer to the underlying array (function template)

### Defect reports

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

| DR | Applied to | Behavior as published | Correct behavior |
| --- | --- | --- | --- |
| CWG 1181 | C++98 | array types could not be value types | they can |
| LWG 208 | C++98 | past-the-end iterators were always non-singular | they can be singular |
| LWG 278 | C++98 | the validity of an iterator was not defined | defined to be always non-singular |
| LWG 324 | C++98 | output iterators had value types | output iterators have writable types instead |
| LWG 407 | C++98 | singular iterators could not be destroyed | allowed |
| LWG 408 (N3066) | C++98 | singular iterators could not be copied | allowed if they are value-initialized |
| LWG 1185 (N3066) | C++98 | *LegacyForwardIterator*, *LegacyBidirectionalIterator* and *LegacyRandomAccessIterator* always satisfy *LegacyOutputIterator* | they might not satisfy *LegacyOutputIterator* |
| LWG 1210 (N3066) | C++98 | the definition of iterator singularity and reachability depended on containers | depend on sequences instead |
| LWG 3009 | C++17 | `<string_view>` did not provide the range access function templates | provides these templates |
| LWG 3987 | C++23 | `<flat_map>` and `<flat_set>` did not provide the range access function templates | provide these templates |

## Podcast Host Debate & Socratic Standard Traps

Use the prompts and tricky ISO standard questions below to guide a Socratic audio discussion:

- Explain why C++20 concepts are not dynamic interfaces or Rust traits, and how atomic constraint subsumption differs from boolean logic.
- Share the infamous missing-nested-requires bug where OnlyInt<double> quietly evaluates to true.
- Trace how std::ranges::advance uses iterator concept subsumption to pick O(1) pointer arithmetic over O(N) increments.

### Trap 1: [temp.constr.atomic] §13.5.1.2 (cppcon-stage)

Why does calling f(42) fail with an ambiguity error in the snippet below?

```cpp
template <class T> requires (sizeof(T) >= 2)
void f(T) {}

template <class T> requires (sizeof(T) >= 2 && std::integral<T>)
void f(T) {}

// f(42); -> Ambiguous! Why?
```

A. sizeof(T) >= 2 is not a constant expression
B. The two sizeof(T) >= 2 expressions are distinct atomic constraints, so the second overload does not subsume the first
C. std::integral<T> cannot combine with sizeof
D. Subsumption only works for class templates, not function templates

**Standard Answer (B):** The two sizeof(T) >= 2 expressions are distinct atomic constraints, so the second overload does not subsume the first

**Why:** Subsumption is syntactic/structural, NOT mathematical theorem proving. Because sizeof(T) >= 2 was written twice inline instead of factored into a single named concept, the compiler treats them as two unrelated atomic constraints.

*CppCon speaker note:* Best single-slide demonstration of what 'atomic constraint normalization' actually means in C++20.

### Trap 2: [expr.prim.req] §7.5.7 (subtle)

Inside concept HasFoo = requires(T t) { t.foo(); };, what happens if t.foo() throws or mutates global state?

A. It runs at startup during static initialization
B. Nothing—a requires-expression is an unevaluated operand evaluated purely at compile time for validity
C. Only constexpr functions are permitted inside requires
D. It compiles only if T is default-constructible

**Standard Answer (B):** Nothing—a requires-expression is an unevaluated operand evaluated purely at compile time for validity

**Why:** Operands of a requires-expression are unevaluated (just like sizeof or decltype). Function parameters like (T t) don't even require T to be constructible or movable—they are hypothetical declarations used solely for type checking.

### Trap 3: [expr.prim.req.nested] §7.5.7.4 (standard-lawyer)

Inside requires(T x) { requires std::same_as<T, int>; }, what happens if you forget the inner 'requires' keyword and write just std::same_as<T, int>;?

```cpp
template <class T>
concept OnlyInt = requires(T x) {
    std::same_as<T, int>; // Missing nested 'requires'!
};
```

A. Same meaning—the compiler checks if std::same_as<T, int> evaluates to true
B. Only checks that the expression std::same_as<T, int> is well-formed (always true for bool!), accepting double and std::string too!
C. Syntax error at compile time
D. Converts x to int

**Standard Answer (B):** Only checks that the expression std::same_as<T, int> is well-formed (always true for bool!), accepting double and std::string too!

**Why:** Without the nested 'requires', a line in a requires-expression is a simple requirement: it checks ONLY whether the expression compiles, NOT whether its boolean value is true! Since std::same_as<double, int> validly yields false (type bool), OnlyInt<double> evaluates to true!

*CppCon speaker note:* One of the deadliest C++20 concept bugs in production code—always use nested `requires` for boolean value checks.

### Trap 4: [temp.res] §13.8 (standard-lawyer)

When calling a dependent member function template on a type parameter T inside a generic function, why is `.template` needed in t.template get<0>()?

```cpp
template <class T>
auto read(T& t) {
    return t.template get<0>();
}
```

A. To tell the linker to emit weak symbols
B. So phase-1 parsing treats '<' as a template argument list instead of a less-than comparison operator
C. Only required in C++11, removed in C++20
D. To force explicit specialization

**Standard Answer (B):** So phase-1 parsing treats '<' as a template argument list instead of a less-than comparison operator

**Why:** During phase-1 template parsing, the compiler does not yet know what T is, so it assumes t.get is a non-template member and parses '<' as less-than (`(t.get < 0) > ()`). Adding `.template` disambiguates the grammar.

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