What is SFINAE
suggest changeSFINAE stands for Substitution Failure Is Not An Error. When substituting template arguments into a function template makes its function type or template parameter declarations ill-formed, that is not a hard compile error. It is only treated as a deduction failure.
A deduction failure removes that function template from consideration, as if that candidate did not exist to begin with.
template <class T>
auto begin(T& c) -> decltype(c.begin()) { return c.begin(); }
template <class T, std::size_t N>
T* begin(T (&arr)[N]) { return arr; }
int vals[10];
begin(vals); // OK. The first function template substitution fails because
// vals.begin() is ill-formed. This is not an error! That function
// is just removed from consideration as a viable overload candidate,
// leaving us with the array overload.
Only substitution failures in the immediate context are considered deduction failures; all others are considered hard errors.
template <class T>
void add_one(T& val) { val += 1; }
int i = 4;
add_one(i); // ok
struct NoAdd {};
NoAdd value;
add_one(value); // error. value += 1 is ill-formed, but this
// failure is NOT in the immediate context of substituting T
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents