Substitution failure is not an error
Substitution failure is not an error (SFINAE) refers to a situation in C++ where an invalid substitution of template parameters is not in itself an error. David Vandevoorde first introduced the acronym SFINAE to describe related programming techniques.[1]
Specifically, when creating a candidate set for overload resolution, some (or all) candidates of that set may be the result of substituting deduced template arguments for the template parameters. If an error occurs during substitution, the compiler removes the potential overload from the candidate set instead of stopping with a compilation error, provided the substitution error is one the C++ standard grants such treatment.[2] If one or more candidates remain and overload resolution succeeds, the invocation is well-formed.
Example
The following example illustrates a basic instance of SFINAE:
struct Test
{
typedef int Type;
};
template < typename T >
void f(typename T::Type) {} // definition #1
template < typename T >
void f(T) {} // definition #2
void foo()
{
f<Test>(10); // call #1
f<int>(10); // call #2 without error thanks to SFINAE
}
Here, attempting to use a non-class type in a qualified name (T::Type
) results in a deduction failure for f<int>
because int
has no nested type named Type
, but the program is well-formed because a valid function remains in the set of candidate functions.
Although SFINAE was introduced to avoid creating ill-formed programs when unrelated template declarations were visible (e.g., through the inclusion of a header file), many developers found the behavior useful for compile-time introspection. Specifically, it allows a template to determine certain properties of its template arguments at instantiation time. Developers of Boost used SFINAE to great effect in boost::enable_if[3] and in other ways.
References
- ↑ Vandevoorde, David; Nicolai M. Josuttis (2002). C++ Templates: The Complete Guide. Addison-Wesley Professional. ISBN 0-201-73484-2.
- ↑ International Organization for Standardization. "ISO/IEC 14882:2003, Programming languages — C++", § 14.8.2.
- ↑ Boost Library List, Find Enable_if on the page for the latest documentation link.
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...