I've seen various SFINAE-related answers regarding calling a function conditionally depending on whether or not a class has a certain function. They're mostly different from what I'm trying to achieve, so I haven't been able to translate it into my project.
I have an algorithm that iterates through a collection and does some stuff. The algo relies on the collection being sorted. I want to be able to feed the algo either a set (which is sorted internally and does NOT have a "sort" function), or a list (which is not sorted, and DOES have a "sort" function).
I want to make my solution more generic than just for set or list. I want there to be a single function which will call the sort method on the template type if it exists, and not otherwise.
template <class Container>
void algo(Container container) {
container.sort();
algoHelper(container);
}
template <class Container>
void algo(Container container) {
algoHelper(contiainer);
}
I thought if I feed a std::set to the first function it'll fail to instantiate because std::set::sort doesn't exist, then it'll attempt to instantiate the second one and succeed.
I also tried adding std::enable_if to the template of the first function:
template <typename Container,
typename std::enable_if_t<std::is_member_function_pointer<decltype(&Container::sort)>::type>>
but got:
error: no matching function for call to 'algo(std::__cxx11::list<int>)'
Not sure how to proceed. Thanks!