I have a templated function, say, a sort function like this
template <typename Iter>
void sort1(Iter first, Iter last) {...}
template <typename Iter, typename Comp>
void sort1(Iter first, Iter last, Comp comp) {...}
where Iter
could be an iterator or a pointer.
I have another templated function that uses distinct Comps in different cases:
template <typename Iter>
void func_using_sort(Iter first, Iter last, int cond) {
if (cond == 0) {
sort1(first, last, [](const auto& a, const auto& b) {
// code 1
});
} else if (cond == 1) {
sort1(first, last, [](const auto& a, const auto& b) {
// code 2
});
} else if (cond == 2) {
sort1(first, last);
}
}
Now I would like to generalize func_using_sort
to use any sort function that has the same interface as sort1
, say,
template <typename Iter>
void sort2(Iter first, Iter last) {...}
template <typename Iter, typename Comp>
void sort2(Iter first, Iter last, Comp comp) {...}
Is there an elegant way to achieve this?