Skip to content

Instantly share code, notes, and snippets.

@Riyaaaaa
Last active August 21, 2016 05:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Riyaaaaa/d7dff56e72c2bfa302933c0cdcd6dc39 to your computer and use it in GitHub Desktop.
【SFINAE】可変リストによるテンプレート関数の優先実体化 ref: http://qiita.com/Riyaaaa_a/items/a156383f640263f35985
template<class T> struct is_const : public std::false_type {};
template<class T> struct is_const<T const> : public std::true_type {};
#include <type_traits>
#include <iostream>
template<class T>
auto printBasicType()
-> typename std::enable_if<std::is_null_pointer<T>{}>::type
{
std::cout << "null pointer" << std::endl;
}
template<class T>
auto printBasicType()
-> typename std::enable_if<std::is_array<T>{}>::type
{
std::cout << "array" << std::endl;
}
template<class T>
auto printBasicType()
-> typename std::enable_if<std::is_integral<T>{}>::type
{
std::cout << "integral" << std::endl;
}
template<class T>
auto printBasicType()
-> typename std::enable_if<std::is_class<T>{}>::type
{
std::cout << "class" << std::endl;
}
int main() {
class Hoge {};
printBasicType<std::nullptr_t>();
printBasicType<int>();
printBasicType<int[]>();
printBasicType<Hoge>();
return 0;
}
extern void* enabler;
template<class T, typename std::enable_if<std::is_null_pointer<T>{}>::type*& = enabler>
void printBasicType()
{
std::cout << "null pointer" << std::endl;
}
//...同様
template<class T, typename std::enable_if<std::is_null_pointer<T>{}, std::nullptr_t>::type = nullptr>
void printBasicType()
{
std::cout << "null pointer" << std::endl;
}
template<typename T, typename = decltype(std::declval<T>().hoge())>
struct has_hoge : std::true_type {};
template<typename T>
struct has_hoge : std::false_type {};
template<class T>
struct has_hoge {
template<typename U = T, typename = decltype(std::declval<U>().hoge()) >
static std::true_type test();
static std::false_type test();
};
void test(...);
template<typename Args...>
void test(Args... args);
#include <type_traits>
template<class T>
struct has_hoge {
template<typename U = T, typename = decltype(std::declval<U>().hoge()) >
static std::true_type test(int);
static std::false_type test(...);
};
int main() {
struct piyo {
bool hoge(){ return true; }
};
static_assert(decltype(has_hoge<piyo>::test(0)){}, "!!");
static_assert(!decltype(has_hoge<int>::test(0)){}, "!!");
return 0;
}
template<class T>
struct identity {
typedef T type;
};
//...
static_assert(identity<decltype(has_hoge<piyo>::test(0))>::type::value, "!!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment