I can\'t clearly get the grasp of what it means when one mentions that a particular function, struct or ... is SFINAE-friendly.
Would someone please
When it allows substitution failure without hard error (as static_assert
).
for example
template
void call_f(const T& t)
{
t.f();
}
The function is declared for all T
, even those with don't have f
, so you cannot do SFINAE on call_f
as the method does exist. (Demo of non compiling code).
With following change:
template
auto call_f(const T& t) ->decltype(t.f(), void())
{
t.f();
}
The method exists only for valid T. so you can use SFINAE as
template
auto call_f_if_available_impl(const T& t, int) -> decltype(call_f(t))
{
call_f(t);
}
template
auto call_f_if_available_impl(const T& t, ...)
{
// Do nothing;
}
template
auto call_f_if_available(const T& t)
{
call_f_if_available_impl(t, 0);
}
Note the int = 0
and ...
is to order the overload.
Demo
--
An other case is when the template add special parameter to apply SFINAE for specialization:
template struct S;
And then
// Specialization only available for T which respect the traits.
template
struct S::value>>
{
};