In order to get an “easier-to-remember” interface to the
index-generating function std::distance(a,b), I came up
with the idea of a better distinction of it’s arguments
(when used against the base of a vector: vec.begin() )
by calling a templated function with the vector
and its iterator, like:
std::vector<MyType> vect;
std::vector<MyType>::const_iterator iter;
...
...
size_t id = vectorindex_of(iter, vect);
with the rationale of never confusing the order of
the arguments 😉
The explicit formulation of the above idea would
read sth. like
template <typename T>
inline
size_t vectorindex_of(
typename std::vector<T>::const_iterator iter,
const std::vector<T>& vect ) {
return std::distance( vect.begin(), iter );
}
… which works but looks awkward.
I’d love to have the template mechanism implicitly deduce the types
like (pseudo-code):
template <typename T>
inline
size_t vectorindex_of(T::const_iterator iter, const T& vect) {
return std::distance( vect.begin(), iter );
}
… which doesn’t work. But why?
The fix is easy: add
typenamebeforeT::const_iterator iter. This is needed because class templates may be specialized and usingtypenametells the compiler a type name is expected atT::const_iteratorand not a value or something.You do the same in your less generic function, too.