Content deleted Content added
Mx. Granger (talk | contribs) {{C++ programming language}} |
No edit summary |
||
(8 intermediate revisions by 6 users not shown) | |||
Line 1:
{{Short description|C++ programming technique}}
'''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==
Line 22 ⟶ 23:
f<int>(10); // Call #2. Without error (even though there is no int::foo)
// thanks to SFINAE.
return 0;
}
</syntaxhighlight>
Line 35 ⟶ 37:
template <typename T>
struct
// Types "yes" and "no" are guaranteed to have different sizes,
// specifically sizeof(yes) == 1 and sizeof(no) == 2.
Line 53 ⟶ 55:
};
struct
typedef float foobar;
};
Line 59 ⟶ 61:
int main() {
std::cout << std::boolalpha;
std::cout <<
std::cout <<
return 0;
}
</syntaxhighlight>
Line 72 ⟶ 75:
#include <iostream>
#include <type_traits>
template <typename T, typename = void>
struct
template <typename T>
struct
struct
using foobar = float;
};
Line 88:
int main() {
std::cout << std::boolalpha;
std::cout <<
std::cout <<
return 0;
}
</syntaxhighlight>
Line 99 ⟶ 100:
template <typename T>
using
struct
using foobar = float;
};
Line 107 ⟶ 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 119 ⟶ 121:
{{C++ programming language}}
▲{{use dmy dates|date=January 2012}}
[[Category:C++]]
[[Category:Articles with example C++ code]]
|