Named requirements
Written expectations about C++ types and objects
Used in the C++ standard to state those expectations
One requirement can be used to define another
Usually paired with some helpers that express those expectations in the code
en.cppreference.com/w/cpp/named_req
DefaultConstructible
Predicate
Iterator categories
LegacyIterator
The base set of requirements for all iterators
- CopyConstructible
- CopyAssignable
- Destructible
- Swappable - std::swap, std::iter_swap
- std::iterator_traits<Iterator>
- *iter - dereferenceable (unspecified return value)
- ++iter - defined and returns Iterator&
std::iterator_traits<I>
LegacyInputIterator
- EqualityComparable
- i != j - valid expression
- *i returns reference from iterator_traits
- reference might be a proxy object
- operator->() is defined
- *it++ returns value_type
- intended for single-pass algorithms - std::find, std::count
Input iterators be like
src: meme-arsenal.com/memes/bda3710e8c0f496156f70ee6b76a6b36.jpg
LegacyForwardIterator
- DefaultConstructible
- std::iterator_traits<Iter>::reference defined as T& or const T&
- i++ returns Iterator (not Iterator& )
- *i++ returns reference
- Provides multipass guarantee
Multipass guarantee
Forward iterators be like
src: carbide-red.com/comp/double_drive.jpg
LegacyBidirectionalIterator
- --i returns Iterator&
- i-- return value convertible to const Iterator&
- *i-- returns reference
Used by std::map and std::set
But also std::reverse_copy and std::random_shuffle
LegacyRandomAccessIterator
- both {i += n}, {i -= n} return Iterator&
- The complexity of the expressions above is O(1)
- both {i + n}, {i - n} return Iterator
- i - j returns difference_type
- i[n] returns reference
- all comparison operators are defined
Used by std::vector