Naveen
Naveen

Reputation: 1523

c++: templates for Comparators throws an error

is there anything wrong with this function prototype? I am trying to pass less as the default comparator function.

template<class iter, typename T, class compare>
void bubble_sort(iter first, iter last, compare cmp = less<T>)

the compiler throws an error saying:

expected primary-expression before ‘)’ token
 void bubble_sort(iter first, iter last, compare cmp = less<T>)

Upvotes: 0

Views: 29

Answers (1)

super
super

Reputation: 12978

less<T> is a type, but you need to give an object of that type as the default argument.

template<class iter, typename T, class compare>
void bubble_sort(iter first, iter last, compare cmp = less<T>{})

If you want to be able to specify T explicitly, the best way is to swap the order of your template arguments, putting T first.

template<typename T, class iter, class compare>
void bubble_sort(iter first, iter last, compare cmp = less<T>{})

Now you can call it and only explicitly specify T like so

bubble_sort<int>(some_container.begin(), some_container.end());

Upvotes: 1

Related Questions