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{});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With