C++ template won't accept iterators

前端 未结 1 1543
萌比男神i
萌比男神i 2020-12-07 05:26

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

template 
void          


        
相关标签:
1条回答
  • 2020-12-07 05:41

    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.

    0 讨论(0)
提交回复
热议问题