C++ template won't accept iterators

2019-01-15 21:57发布

问题:

I'm re-learning C++, and have started by trying what should be a simple algorithm: QuickSort. My function has this signature:

template <class T>
void QSort(typename std::vector<T>::iterator begin, typename std::vector<T>::iterator end)

And it is called in my main function:

int main()
{
    std::vector<int> unsort({56,32,11,45,67,81,12,5});
    std::vector<int>::iterator b=unsort.begin();
    std::vector<int>::iterator e=unsort.end();
    QSort(b, e);
    return 0;
}

And gives this error:

C:\Users\Deus\Projects\QSort\main.cpp||In function 'int main()':|
C:\Users\Deus\Projects\QSort\main.cpp|49|error: no matching function for call to 'QSort(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&)'|
||=== Build finished: 1 errors, 0 warnings ===|

It seems that the compiler is having trouble resolving what T should be. Is there a way to do what I'm trying to do, or should I just declare the arguments as type T, and work with the resulting uncertainty?

回答1:

The compiler has no way to deduce T from your function call. Think about what happens when std::vector<T>::iterator is T*:

int *b = ...;
int *e = ...;
QSort(b, e);

In general, if you write typename Something<TemplateParameter>::anotherThing, then the TemplateParemter cannot be deduced in the call. It must be explicitly provided

QSort<int>(b, e);

I recommend to just use T as the parameter type. That will allow you to not only accept vector iterators, but also T*, or std::deque<T>::iterator and any other random access iterators.