Last active
August 29, 2015 14:24
-
-
Save takumakei/ca624320fe0e889b5c24 to your computer and use it in GitHub Desktop.
SFINAE
This file contains hidden or 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
#include <type_traits> | |
namespace sfinae { | |
template<bool b> using where = typename std::enable_if<b, std::nullptr_t>::type; | |
template<class A, class = std::nullptr_t> | |
struct case_t { | |
static char const* label() { return "cond-default"; } | |
}; | |
template<> | |
struct case_t<bool> { | |
static char const* label() { return "bool"; } | |
}; | |
template<class A> | |
struct case_t<A, where<std::is_integral<A>{}>> { | |
static char const* label() { return "cond-integral"; } | |
}; | |
template<class A> | |
struct case_t<A, where<std::is_floating_point<A>{}>> { | |
static char const* label() { return "cond-floating point"; } | |
}; | |
template<class A, where<!std::is_integral<A>{} && !std::is_floating_point<A>{}> = nullptr> | |
char const* label() { return "default"; } | |
template<class A, where<std::is_same<A, bool>{}> = nullptr> | |
char const* label() { return "bool"; } | |
template<class A, where<std::is_integral<A>{} && !std::is_same<A, bool>{}> = nullptr> | |
char const* label() { return "integral"; } | |
template<class A, where<std::is_floating_point<A>{}> = nullptr> | |
char const* label() { return "floating point"; } | |
} // namespace | |
#include <iostream> | |
using namespace std; | |
int main() { | |
cout << sfinae::case_t<nullptr_t>::label() << endl; | |
cout << sfinae::case_t<bool >::label() << endl; | |
cout << sfinae::case_t<int >::label() << endl; | |
cout << sfinae::case_t<float >::label() << endl; | |
cout << sfinae::label<nullptr_t>() << endl; | |
cout << sfinae::label<bool >() << endl; | |
cout << sfinae::label<int >() << endl; | |
cout << sfinae::label<float >() << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment