Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why differentiate predicate and non-predicate versions for generic algorithms?

The standard library does differentiate predicate and non-predicate versions of generic algorithms. For example, std::sort() looks like:

template< class RandomIt >
void sort( RandomIt first, RandomIt last );

template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );

Is there any problem in just writing the following?

template< class RandomIt, class Compare = std::less<void>>
void sort( RandomIt first, RandomIt last, Compare comp = Compare{});
like image 691
Lingxi Avatar asked Jan 28 '16 08:01

Lingxi


1 Answers

Pretty much historical reasons.

C++98/03 didn't have default template arguments for function templates, so it had to use two overloads. And changing it afterwards can break user code.

So, suppose we just redesign the whole thing, should the second form be preferred?

That's what the current Ranges TS working draft does.

like image 54
T.C. Avatar answered Nov 15 '22 05:11

T.C.