I am using function SFINAE heavily in a project and am not sure if there are any differences between the following two approaches (other than style):
#include &l
I have seen method 2 used more often in stackoverflow, but I prefer method 1.
Suggestion: prefer method 2.
Both methods work with single functions. The problem arises when you have more than a function, with the same signature, and you want enable only one function of the set.
Suppose that you want enable foo()
, version 1, when bar
(pretend it's a constexpr
function) is true
, and foo()
, version 2, when bar
is false
.
With
template ()>>
void foo () // version 1
{ }
template ()>>
void foo () // version 2
{ }
you get a compilation error because you have an ambiguity: two foo()
functions with the same signature (a default template parameter doesn't change the signature).
But the following solution
template (), bool> = true>
void foo () // version 1
{ }
template (), bool> = true>
void foo () // version 2
{ }
works, because SFINAE modify the signature of the functions.
Unrelated observation: there is also a third method: enable/disable the return type (except for class/struct constructors, obviously)
template
std::enable_if_t()> foo () // version 1
{ }
template
std::enable_if_t()> foo () // version 2
{ }
As method 2, method 3 is compatible with selection of alternative functions with same signature.