It's a complicated topic and is not easy give an answer at your question.
Anyway, some observations/suggestions, without any pretense to being exhaustive.
(1) the static_assert()
way
static_assert(LessThanComparable<U,T>, "oh this is not epic");
is a good solution if you want a function that works only with some types and give an error (a clear error, because you can choose the error message) if called with wrong types.
But usually is the wrong solution when you want an alternative. Isn't a SFINAE solution. So calling the function with arguments of wrong types gives an error and doesn't permit that another function is used in substitution.
(2) You're right about the
typename = std::enable_if_t</* some test */>>
solution. An user can explicit a third parameter by hand. Joking, I say that this solution can be "hijacked".
But isn't the only drawback of this solution.
Suppose you have two complementary foo()
functions that have to be enabled/disabled via SFINAE; the first one when a test is true, the second one when the same test if false.
You can think that the following solution is dangerous (can be hijacked) but can work
/* true case */
template <typename T, typename = std::enable_if_t<true == some_test_v<T>>>
void foo (T t)
{ /* something with t */ }
/* false case */
template <typename T, typename = std::enable_if_t<false == some_test_v<T>>>
void foo (T t)
{ /* something with t */ }
Wrong: this solution simply doesn't works because you're enabling/disabling not the second typename but only the default value for the second typename. So you're not completely enabling/disabling the functions and the compiler have to consider two functions with the same signature (the signature of a function doesn't depends from default values); so you have a collision and obtain an error.
The following solutions, SFINAE over the returned type
std::enable_if_t<LessThanComparable<U,T>, void> order(T& a, U& b)
(also without void
, that is the default type
std::enable_if_t<LessThanComparable<U,T>> order(T& a, U& b)
) or over the second type (following the Yakk's suggestion about a not-standard allowed void *
)
template <typename T, typename U,
std::enable_if_t<LessThanComparable<U,T>>, bool> = true>
are (IMHO) good solutions because both of they avoid the hijacking risk and are compatible with two complementary functions with the same name and signature.
I suggest a third possible solution (no hijack-able, complementary compatible) that is the addition of a third defaulted value with SFINAE enabled/disabled type: something as
template <typename T, typename U>
void order(T& a, U& b, std::enable_if_t<LessThanComparable<U,T>> * = nullptr)
Another possible solution avoid at all SFINAE but use tag-dispatching; something as
template <typename T, typename U>
void order_helper (T & a, U & b, std::true_type const &)
{ if (b < a) { std::swap(a, b); } }
// something different if LessThanComparable is false ?
template <typename T, typename U>
void order_helper (T & a, U & b, std::false_type const &)
{ /* ???? */ }
template <typename T, typename U>
void order (T & a, U & b)
{ order_helper(a, b, LessThanComparable<U,T>{}); }
This in case LessThanComplarable
inherit from std::true_type
when the condition is true, from std::false_type
when the condition is false.
Otherwise, if LessThanComparable
gives only a boolean value, the call to order_helper()
can be
order_helper(a, b, std::integral_constant<bool, LessThanComparable<U,T>>{});
(3) if you can use C++17, there is the if constexpr
way that can avoid a lot of overloading
template <typename T, typename U>
void order(T& a, U& b)
{
if constexpr ( LessThanComparable<U, T> )
{
if ( b < a )
std::swap(a, b);
}
else
{
// what else ?
}
}
LessThanComparable
? – Attah