Presumed value and difference types of an iterator type in ranges and non-ranges algorithms
Jiang An

Created on 2024-04-26.00:00:00 last changed 1 month ago


Date: 2024-04-27.16:27:25

Given an iterator type I, currently both std::iter_value_t<I> and std::iterator_traits<I>::value_type can be called the value type of I. These two types can be different if one specializes std::indirect_readable_traits for I such that std::indirect_readable_traits<I>::value_type is different from I::value_type.

It seems intended that ranges algorithms use std::iter_value_t<I> and non-ranges algorithms, along with other legacy iterator-related mechanisms, use std::iterator_traits<I>::value_type. But this doesn't seem clarified anywhere.

P2408R5 made the situation more complicated. In C++23, the type requirements of non-mutating iterators passed to many legacy algorithms were changed to C++20 iterator concepts, which are strongly associated with std::iter_value_t. It didn't seem intended that implementations were expected to use possibly different value types since C++23, but such reading might be suggested by the use of concepts.

Likewise, a non-random-access iterator type I can have two different difference types if one specializes std::incrementable_traits for I such that std::incrementable_traits<I>::difference_type is different from I::difference_type. It's also unclear which one should be used for non-mutating iterator in algorithms.

Date User Action Args
2024-04-26 00:00:00admincreate