为什么要区分通用算法的谓词和非谓词版本?
Why differentiate predicate and non-predicate versions for generic algorithms?
标准库确实区分了通用算法的谓词和非谓词版本。例如,std::sort()
看起来像:
template< class RandomIt >
void sort( RandomIt first, RandomIt last );
template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );
下面这样写有问题吗?
template< class RandomIt, class Compare = std::less<void>>
void sort( RandomIt first, RandomIt last, Compare comp = Compare{});
历史原因比较多。
C++98/03 没有函数模板的默认模板参数,所以它必须使用两个重载。之后更改它可能会破坏用户代码。
So, suppose we just redesign the whole thing, should the second form
be preferred?
这就是 current Ranges TS working draft 的作用。
标准库确实区分了通用算法的谓词和非谓词版本。例如,std::sort()
看起来像:
template< class RandomIt >
void sort( RandomIt first, RandomIt last );
template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );
下面这样写有问题吗?
template< class RandomIt, class Compare = std::less<void>>
void sort( RandomIt first, RandomIt last, Compare comp = Compare{});
历史原因比较多。
C++98/03 没有函数模板的默认模板参数,所以它必须使用两个重载。之后更改它可能会破坏用户代码。
So, suppose we just redesign the whole thing, should the second form be preferred?
这就是 current Ranges TS working draft 的作用。