Substitution failure is not an error: Difference between revisions

Content deleted Content added
C++Bro123 (talk | contribs)
m Improve C++ examples
Undid revision 905156807 by C++Bro123 (talk): How are these changes improvements? (Especially the changed indentation.)
Line 8:
<source lang="cpp">
struct Test {
typedef int foo;
};
 
template <typename T>
void f(typename T::foo) {} // Definition #1
 
template <typename T>
void f(T) {} // Definition #2
 
int main() {
f<Test>(10); // Call #1.
f<int>(10); // Call #2. Without error (even though there is no int::foo) thanks to SFINAE.
// thanks to SFINAE.
}
</source>
Line 35 ⟶ 34:
template <typename T>
struct has_typedef_foobar {
// Types "yes" and "no" are guaranteed to have different sizes,
// specifically sizeof(yes) == 1 and sizeof(no) == 2.
typedef char yes[1];
typedef char no[2];
 
template <typename C>
static yes& test(typename C::foobar*);
 
template <typename>
static no& test(...);
 
// If the "sizeof" of the result of calling test<T>(nullptr) is equal to sizeof(yes),
// sizeof(yes), the first overload worked and T has a nested type named foobar.
static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
// foobar.
static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
};
 
struct foo {
typedef float foobar;
};
 
int main() {
std::cout << std::boolalpha;
std::cout << has_typedef_foobar<int>::value << std::endl; // Prints false
std::cout << has_typedef_foobar<foo>::value << std::endl; // Prints true
}
</source>
Line 72 ⟶ 70:
#include <type_traits>
 
template <typename... Ts> using void_t = void;
using void_t = void;
 
template <typename T, typename = void>