9 Iterators library [iterators]

9.3 Iterator requirements [iterator.requirements]

9.3.3 Iterator associated types [iterator.assoc.types]

9.3.3.1 difference_type [iterator.assoc.types.difference_type]

difference_type_t<T> is implemented as if:

  template <class> struct difference_type { };

  template <class T>
  struct difference_type<T*>
    : enable_if<is_object<T>::value, ptrdiff_t> { };

  template <class I>
  struct difference_type<const I> : difference_type<decay_t<I>> { };

  template <class T>
    requires requires { typename T::difference_type; }
  struct difference_type<T> {
    using type = typename T::difference_type;
  };

  template <class T>
    requires !requires { typename T::difference_type; } &&
      requires(const T& a, const T& b) { { a - b } -> Integral; }
  struct difference_type<T>
    : make_signed< decltype(declval<T>() - declval<T>()) > {
  };

  template <class T> using difference_type_t
    = typename difference_type<T>::type;

Users may specialize difference_type on user-defined types.