Std :: vector and its iterator as one template template name

To get the “easier to remember” interface, the index-generating function std :: distance (a, b), I came up with the idea of ​​better delimiting its arguments (when using against the vector base: vec.begin ()) calling a template function with a vector and its iterator, for example:

std::vector<MyType> vect;
std::vector<MyType>::const_iterator iter;
...
...
size_t id = vectorindex_of(iter, vect);

with justification, never confuse the order of arguments; -)

Explicit formulation of the above idea read the article. as

 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 uncomfortable.

I would like the template engine to implicitly infer types (pseudocode):

 template <typename T>
 inline 
 size_t vectorindex_of(T::const_iterator iter, const T& vect) {
    return std::distance( vect.begin(), iter ); 
 }

... which does not work. But why?

+3
source share
3 answers

: typename T::const_iterator iter. , typename , T::const_iterator, - .

.

+8
 template <typename T>
 inline 
 std::size_t vectorindex_of(typename T::const_iterator iter, const T& vect) {
    return std::distance( vect.begin(), iter ); 
 }

( typename). .

+2

" " :

i - vec.begin()

!

+1

Source: https://habr.com/ru/post/1718890/


All Articles