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
In addition to max66's answer, another reason to prefer method 2 is that with method 1, you can (accidentally) pass an explicit type parameter as the second template argument and defeat the SFINAE mechanism completely. This could happen as a typo, copy/paste error, or as an oversight in a larger template mechanism.
#include
#include
#include
// NOTE: foo should only accept T=int
template >>
void foo(){
std::cout << "method 1" << std::endl;
}
int main(){
// works fine
foo();
// ERROR: subsitution failure, as expected
// foo();
// Oops! also works, even though T != int :(
foo();
return 0;
}
Live demo here