c++ - default value for template function, functional parameter -
template<typename iterator, typename typename comparator = std::less<typename std::iterator_traits<iterator>::value_type>> static void sort(iterator begin, iterator end, comparator cmp = comparator()) { ... } i have following template function:
template<typename func> static void sort_test(func sort) { ... sort(somevector.begin(), somevector.end()); ... } int main() { sort_test(&sort<vector<int, allocator<int>>::iterator>); return 0; } error c2198: 'void (__cdecl *)(iterator,iterator,std::less)' : few arguments call
if try bypass default argument providing it:
template<typename func> static void sort_test(func sort) { ... sort(somevector.begin(), somevector.end(), std::less<int>); ... } int main() { sort_test(&sort<vector<int, allocator<int>>::iterator>, std::less<int>); return 0; } error c2275: 'std::less' : illegal use of type expression
default arguments not part of function's signature, when go through pointer function indirection, you're doing in first example, information lost. if call sort function directly within main 2 iterator arguments, code compile.
in second example you're getting compilation error because you're trying pass type, instead of instance, sort
sort(somevector.begin(), somevector.end(), std::less<int>()); you have typename in template parameter list sort
template<typename iterator, typename typename comparator = std::less<typename std::iterator_traits<iterator>::value_type>> // ^^^^^^^^
Comments
Post a Comment