C++ template won't accept iterators

前端 未结 1 1540
萌比男神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::iterator is T*:

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

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

    QSort(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::iterator and any other random access iterators.

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