Substitution failure is not an error: Difference between revisions

Content deleted Content added
Erdavila (talk | contribs)
Example: Fixing consitency
Line 62:
 
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>. (An ellipsis is used not only because it will accept any argument, but also because its conversion rank is lowest, so a call to the first function will be preferred if it is possible; this removes ambiguity.)
 
In C++11, the above code could be simplified to:
 
<source lang="cpp">
#include <iostream>
#include <type_traits>
 
template <typename... Ts> using void_t = void;
 
template <typename T, typename = void>
struct has_typedef_foobar : std::false_type {};
 
template <typename T>
struct has_typedef_foobar<T, void_t<typename T::foobar>> : std::true_type {};
 
struct foo {
using foobar = float;
};
 
int main() {
std::cout << std::boolalpha;
std::cout << has_typedef_foobar<int>::value << std::endl;
std::cout << has_typedef_foobar<foo>::value << std::endl;
}
</source>
 
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.