Home > Mobile >  Must difference_type be comparable?
Must difference_type be comparable?

Time:10-18

The requirements for random access iterators are found here. On this page, you will see that for any two RandIts, a and b, a<b and a-b are both legal c . a-b returns a difference_type.

In my code, I want to compute a<b, but instead of comparing a and b, I want to compare a-first and b-first. This requires two things:

  1. difference_types are comparable.
  2. (a-first < b-first) == (a < b).

It would be easy to implement comparison of difference_types as satisfying (2), but I can't tell if this is required by the standard. Do (1) and (2) hold?

CodePudding user response:

The standard requires iterator_traits<It>::difference_type to be a "signed integer type" (or void). This statement is to be taken literally. [basic.fundamental]/1 defines a number of types which are "integer types" and a subset of them to be "signed integer types".

These are the only "signed integer types" in C . You may create types which act like "signed integer types," but the standard clearly defines a specific set of functional types which fit this term. And your user-defined types aren't them.

So it's not your choice whether difference_type is "comparable" or not; all integer types are comparable because that's how C defines them.

  • Related