Skip to main content

C++ named requirements: LegacyIterator

The LegacyIterator requirements describe types that can be used to identify and traverse the elements of a container.

LegacyIterator is the base set of requirements used by other iterator types: LegacyInputIterator, LegacyOutputIterator, LegacyForwardIterator, LegacyBidirectionalIterator, and LegacyRandomAccessIterator. Iterators can be thought of as an abstraction of pointers.

All the categories of iterators require only those functions that are realizable for a given category in constant time (amortized). Therefore, requirement tables and concept definitions (since C++20) for the iterators do not specify complexity.

Requirements

The type It satisfies LegacyIterator if

Given

  • r, an lvalue of type It, the following expressions must be valid and have their specified effects:
ExpressionReturn TypePrecondition
*runspecifiedr is dereferenceable
++rIt&r is incrementable (the behavior of the expression ++r is defined)

Concept (since C++20)

Click to expand

For the definition of std::iterator_traits, the following exposition-only concept is defined.

template<class I>
concept __LegacyIterator =
requires(I i)
{
{ *i } -> __Referenceable;
{ ++i } -> std::same_as<I&>;
{ *i++ } -> __Referenceable;
} && std::copyable<I>;

where the exposition-only concept __Referenceable<T> is satisfied if and only if T& is a valid type (in particular, T must not be void).

Defect Reports

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

DRApplied toBehavior as publishedCorrect behavior
LWG 3420C++20the exposition-only concept checks copyable firstcopyable is checked only if the requires-expression yields true

C++ named requirements: LegacyIterator

The LegacyIterator requirements describe types that can be used to identify and traverse the elements of a container.

LegacyIterator is the base set of requirements used by other iterator types: LegacyInputIterator, LegacyOutputIterator, LegacyForwardIterator, LegacyBidirectionalIterator, and LegacyRandomAccessIterator. Iterators can be thought of as an abstraction of pointers.

All the categories of iterators require only those functions that are realizable for a given category in constant time (amortized). Therefore, requirement tables and concept definitions (since C++20) for the iterators do not specify complexity.

Requirements

The type It satisfies LegacyIterator if

Given

  • r, an lvalue of type It, the following expressions must be valid and have their specified effects:
ExpressionReturn TypePrecondition
*runspecifiedr is dereferenceable
++rIt&r is incrementable (the behavior of the expression ++r is defined)

Concept (since C++20)

Click to expand

For the definition of std::iterator_traits, the following exposition-only concept is defined.

template<class I>
concept __LegacyIterator =
requires(I i)
{
{ *i } -> __Referenceable;
{ ++i } -> std::same_as<I&>;
{ *i++ } -> __Referenceable;
} && std::copyable<I>;

where the exposition-only concept __Referenceable<T> is satisfied if and only if T& is a valid type (in particular, T must not be void).

Defect Reports

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

DRApplied toBehavior as publishedCorrect behavior
LWG 3420C++20the exposition-only concept checks copyable firstcopyable is checked only if the requires-expression yields true