Re: work round for std::distance's long arm.
Louis Lavery wrote:
Without the nested typedefs the following fails to compile
under vc7 and gcc 3.2.
/* distance.cpp */
#include <iterator>
#include <list>
namespace user
{
template<typename Iter,typename Dist> struct Cursor
{
#if 0
typedef void iterator_category;
typedef void difference_type;
typedef void value_type;
typedef void reference;
typedef void pointer;
#endif
};
template<typename Iter,typename Dist>
Dist distance(
Cursor<Iter,Dist> const&,
Cursor<Iter,Dist> const&)
{
return 0;
}
}
int main()
{
user::Cursor<std::list<int>::iterator,int> cur;
distance(cur,cur); // XXX
return 0;
}
/* distance.cpp end */
Because Cursor's instantiated with an iterator from std,
I guess what happens (at XXX) is the compiler looks in
namespace std and sees something like...
template<class Iter>
typename iterator_traits<Iter>::difference_type
distance<Iter,Iter) {...}
...and so needs to instantiate iterator_traits<Cursor> to get
the return type. But the default iterator_traits requires its
parameter to have nested typedefs for iterator_category etc.
Have I got that right?
That's correct. For example:
std::iterator_traits<_Iter>::iterator_category is attempting to use
_Iter=user::Cursor<std::list<int>::_Iterator<true>,int>.
This being the case, what's the best work round?
I know I can use void typedefs as above but that adds noise.
Is there a better way, other than not using the name distance?
Thanks, Louis.
Any particular reason to not use:
user::distance(cur,cur);
?
Cheers,
Radu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]