Overload resolution with a large number of options
suggest changeIf you need to select between several options, enabling just one via enable_if<> can be quite cumbersome, since several conditions need to be negated too.
The ordering between overloads can instead be selected using inheritance, i.e. tag dispatch.
Instead of testing for the thing that needs to be well-formed, and also testing the negation of all the other versions conditions, we instead test just for what we need, preferably in a decltype in a trailing return. This might leave several options well-formed. We differentiate between those using tags, similar to iterator-trait tags (random_access_tag and the rest). An exact match is better than a conversion to a base, and a conversion to a nearer base is better than a conversion to a more distant base.
#include <algorithm>
#include <iterator>
namespace detail
{
// this gives us infinite types, that inherit from each other
template<std::size_t N>
struct pick : pick<N-1> {};
template<>
struct pick<0> {};
// the overload we want to be preferred has a higher N in pick<N>
// this is the first helper template function
template<typename T>
auto stable_sort(T& t, pick<2>)
-> decltype( t.stable_sort(), void() )
{
// if the container has a member stable_sort, use that
t.stable_sort();
}
// this helper will be second best match
template<typename T>
auto stable_sort(T& t, pick<1>)
-> decltype( t.sort(), void() )
{
// if the container has a member sort, but no member stable_sort.
// std::list::sort is stable; a member sort is not stable in general.
t.sort();
}
// this helper will be picked last
template<typename T>
auto stable_sort(T& t, pick<0>)
-> decltype( std::stable_sort(std::begin(t), std::end(t)), void() )
{
// the container has neither a member sort nor a member stable_sort
std::stable_sort(std::begin(t), std::end(t));
}
}
// this is the function the user calls. it will dispatch the call
// to the correct implementation with the help of 'tags'.
template<typename T>
void stable_sort(T& t)
{
// use an N that is higher than any used above.
// this will pick the highest overload that is well formed.
detail::stable_sort(t, detail::pick<10>{});
}
There are other methods commonly used to differentiate between overloads, such as exact match being better than conversion, being better than ellipsis.
However, tag dispatch can extend to any number of choices, and its intent is a bit clearer.