Created
August 24, 2019 14:06
-
-
Save mkolod/ac43fc3c96612dcb99d41d4a846e65eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// After the Wiki example | |
#include <iostream> | |
#include <type_traits> | |
#include <experimental/type_traits> | |
template <typename T> | |
struct has_foobar1 { | |
typedef char yes[1]; | |
typedef char no[2]; | |
template <typename C> | |
static yes& test(typename C::foobar*); | |
template <typename> | |
static no& test(...); | |
static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes); | |
}; | |
template <typename... Ts> | |
using void_t = void; | |
template <typename T, typename = void> | |
struct has_foobar2 : std::false_type {}; | |
template <typename T> | |
struct has_foobar2 <T, void_t<typename T::foobar>> : std::true_type {}; | |
struct foo { | |
typedef int foobar; | |
}; | |
template <typename T> | |
using has_foobar3 = typename T::foobar; | |
int main() { | |
std::cout << std::boolalpha; | |
std::cout << "\nPre C++11 SFINAE" << std::endl; | |
std::cout << has_foobar1<foo>::value << std::endl; | |
std::cout << has_foobar1<int>::value << std::endl; | |
std::cout << "\nC++11 SFINAE" << std::endl; | |
std::cout << has_foobar2<foo>::value << std::endl; | |
std::cout << has_foobar2<int>::value << std::endl; | |
std::cout << "\nC++11 SFINAE after Library Fundamental v2" << std::endl; | |
std::cout << std::experimental::is_detected<has_foobar3, foo>::value << std::endl; | |
std::cout << std::experimental::is_detected<has_foobar3, int>::value << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
Pre C++11 SFINAE
true
false
C++11 SFINAE
true
false
C++11 SFINAE after Library Fundamental v2
true
false