Skip to content

Instantly share code, notes, and snippets.

@takumakei
Last active August 29, 2015 14:24
Show Gist options
  • Save takumakei/ca624320fe0e889b5c24 to your computer and use it in GitHub Desktop.
Save takumakei/ca624320fe0e889b5c24 to your computer and use it in GitHub Desktop.
SFINAE
#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