Last active
August 21, 2016 05:17
-
-
Save Riyaaaaa/d7dff56e72c2bfa302933c0cdcd6dc39 to your computer and use it in GitHub Desktop.
【SFINAE】可変リストによるテンプレート関数の優先実体化 ref: http://qiita.com/Riyaaaa_a/items/a156383f640263f35985
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
template<class T> struct is_const : public std::false_type {}; | |
template<class T> struct is_const<T const> : public std::true_type {}; |
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> | |
#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; | |
} | |
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
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; | |
} | |
//...同様 | |
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
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; | |
} |
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
template<typename T, typename = decltype(std::declval<T>().hoge())> | |
struct has_hoge : std::true_type {}; | |
template<typename T> | |
struct has_hoge : std::false_type {}; |
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
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(); | |
}; |
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
void test(...); | |
template<typename Args...> | |
void test(Args... args); |
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> | |
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; | |
} |
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
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