Content deleted Content added
added connection words to improve readability |
No edit summary |
||
(21 intermediate revisions by 16 users not shown) | |||
Line 1:
{{Short description|C++ programming technique}}
'''Substitution failure is not an error''' ('''SFINAE''') refers to a situation in [[C++]] where an invalid substitution of [[template (programming)|template]] parameters is not in itself an error. David Vandevoorde first introduced the acronym SFINAE to describe related programming techniques.<ref>{{cite book | last=Vandevoorde | first=David |author2=Nicolai M. Josuttis | title=C++ Templates: The Complete Guide | publisher=Addison-Wesley Professional | year=2002 | isbn=0-201-73484-2}}</ref>▼
▲'''Substitution failure is not an error''' ('''SFINAE''')
Specifically, when creating a candidate set for [[overload resolution]], some (or all) candidates of that set may be the result of instantiated templates with (potentially deduced) template arguments substituted for the corresponding template parameters. If an error occurs during the substitution of a set of arguments for any given template, the compiler removes the potential overload from the candidate set instead of stopping with a compilation error, provided
==Example==
The following example illustrates a basic instance of SFINAE:
<
struct Test {
};
template <typename T>
void f(typename T::foo) {} // Definition #1
template <typename T>
void f(T) {}
int main() {
// thanks to SFINAE. return 0;
}
</syntaxhighlight>
Here, attempting to use a non-class type in a qualified name (<code>T::foo</code>) results in a deduction failure for <code>f<int></code> because <code>int</code> has no nested type named <code>foo</code>, but the program is well-formed because a valid function remains in the set of candidate functions.
Line 29 ⟶ 33:
For example, SFINAE can be used to determine if a type contains a certain typedef:
<
#include <iostream>
template <typename T>
struct
// foobar. };
struct
};
int main() {
return 0;
}
</syntaxhighlight>
When <code>T</code> has the nested type <code>foobar</code> defined, the instantiation of the first <code>test</code> works and the null pointer constant is successfully passed. (And the resulting type of the expression is <code>yes</code>.) If it does not work, the only available function is the second <code>test</code>, and the resulting type of the expression is <code>no</code>.
== C++11 simplification ==
In [[C++11]], the above code could be simplified to: <
#include <iostream>
#include <type_traits>
template <typename T, typename = void>
struct
template <typename T>
struct
struct
using foobar = float;
};
Line 83 ⟶ 88:
int main() {
std::cout << std::boolalpha;
std::cout <<
std::cout <<
return 0;
}
</syntaxhighlight>
With the standardisation of the detection idiom in the [http://en.cppreference.com/w/cpp/experimental/lib_extensions_2 Library fundamental v2 (n4562)] proposal, the above code could be re-written as follows:
<
#include <iostream>
#include <type_traits>
template <typename T>
using
struct
using foobar = float;
};
Line 102 ⟶ 108:
int main() {
std::cout << std::boolalpha;
std::cout << std::is_detected<
std::cout << std::is_detected<
return 0;
}
</syntaxhighlight>
The developers of [[Boost C++ Libraries|Boost]] used SFINAE in boost::enable_if<ref name="enable_if">[http://www.boost.org/doc/libs/release/libs/utility/enable_if.html Boost Enable If]</ref> and in other ways.
Line 112 ⟶ 119:
{{reflist}}
{{C++ programming language}}
▲{{use dmy dates|date=January 2012}}
[[Category:C++]]
[[Category:Articles with example C++ code]]
[[Category:Software design patterns]]
|