Re: work round for std::distance's long arm.
Louis Lavery <Louis@laver.demon.co.uk> writes:
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
Yes, due to argument-dependent lookup. Because
std::list<int>::iterator is a class in namespace std, std becomes an
associated namespace of the argument, and the signature of that
function is considered during lookup.
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?
Exactly.
This being the case, what's the best work round?
Explicit qualification:
user::distance(cur,cur)
Of course, if your real case is a generic function in which the call
to distance is supposed to be a customization point, you'll have to
use a different dispatch method to avoid argument-dependent lookup.
Today there are few good alternatives.
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?
Unfortunately not.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]