Search code examples
c++c++11mergesort

wrong number of template arguments


I want to write template Merge Sort.

    template <class RandomAccessIterator, class Comparator = std::less<>>
void MergeSort(RandomAccessIterator begin, RandomAccessIterator end, Comparator compare) {
    int s = end - begin;
    if (s > 1)
    {
        RandomAccessIterator middle = begin + s / 2 + 1;
        MergeSort(begin, middle, compare);
        MergeSort(middle, end, compare);
        std::inplace_merge(begin, middle, end, compare);
    }
}

And the message I get is: error: wrong number of template arguments (0, should be 1) template >.

I tried to move std::less to function declaration, but also failed. What do I need to do?


Solution

  • Since you tagged your question as C++11, it seem you cannot use diamond operators.

    As you can see in the documentation, the form std::less<> is only allowed since C++14.

    What you can do here is to use the other form:

    std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>