4

I'm trying to find out the significance of the difference_type member for the satisfaction of std::weakly_incrementable concept. I'm currently defining a class satisfying std::output_iterator and have nothing in the domain to attach this type to (and struggling to find a use case for it).

I've tracked the root to n3351 paper and yet haven't found the actual reason there. Why did the commitee choose to put it this place in the hierarchy instead of std::input_iterator for example?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
neonxc
  • 802
  • 9
  • 21
  • What's `difference_type`? Basically, were the iterator to have `-` operator that computes number of `++` needed to get from iterator to another then it would return `difference_type`. You just let it be `std::ptrdiff_t` or just large enough signed integer. – ALX23z Oct 04 '21 at 13:40
  • There are many range iterator operations in the standard that need to use `iter_difference_t`, where `I` can be an `output_iterator`. – 康桓瑋 Oct 04 '21 at 14:13
  • 1
    Related: [How to handle iterator::difference_type when you have no way of measuring the difference?](https://stackoverflow.com/questions/46695349/how-to-handle-iteratordifference-type-when-you-have-no-way-of-measuring-the-di) – ArborealAnole Oct 07 '21 at 08:15

1 Answers1

2

It's used by ranges::advance et.al.

For an output iterator who's ++ is a no-op, it's fine to define it as whichever signed integer type you like. I'd recommend std::ptrdiff_t. See also: std::ostream_iterator::difference_type

Caleth
  • 52,200
  • 2
  • 44
  • 75