Q:

difference_type not found

When I try to use std::distance with a custom iterator under gcc 4.7, it complains about not finding the difference_type. I have sadly no idea why it fails.
#include <iterator>

class nit {
public:
    typedef int difference_type;
};

int main() {
  const nit test1;
  std::distance( test1, test1 );
  return 0;
}

gives the error:
/usr/include/c++/4.7/bits/stl_iterator_base_funcs.h:114:5: error: no type named ‘difference_type’ in ‘struct std::iterator_traits<nit>’

A:

Have you tried defining all the required types/operators?
#include <iterator>

struct nit
{
  typedef std::random_access_iterator_tag iterator_category;
  typedef int value_type;
  typedef int difference_type;
  typedef int* pointer;
  typedef int& reference;

  bool operator==(nit const&)
  {
    return true;
  }

  bool operator!=(nit const&)
  {
    return false;
  }

  int operator-(nit const&)
  {
    return 0;
  }

  nit()
  {
  }
};

int main()
{
  nit const test1;
  std::distance(test1, test1);

  return 0;
}

