Created
July 13, 2013 06:15
-
-
Save nekko1119/5989625 to your computer and use it in GitHub Desktop.
Tがvalue_typeを持っていればvalue_typeを、持っていなければTを返すメタ関数。 参考https://twitter.com/kester44/status/348712647203618816
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
#include <type_traits> | |
template <bool C, class T, class F> | |
struct eval_if_c; | |
template <class T, class F> | |
struct eval_if_c<true, T, F> | |
{ | |
typedef typename T::type type; | |
}; | |
template <class T, class F> | |
struct eval_if_c<false, T, F> | |
{ | |
typedef typename F::type type; | |
}; | |
template <class C, class T, class F> | |
struct eval_if : eval_if_c<C::value, T, F> | |
{}; | |
template <class T> | |
struct value_type | |
{ | |
typedef typename T::value_type type; | |
}; | |
template <class T> | |
struct identity | |
{ | |
typedef T type; | |
}; | |
template <class T, class U = typename T::value_type> | |
std::true_type check(int); | |
template <class T> | |
std::false_type check(long); | |
template <class T> | |
struct has_value_type : decltype(check<T>(0)) {}; | |
template <class T> | |
struct value_type_or_identity | |
: eval_if<has_value_type<T>, value_type<T>, identity<T>> | |
{}; | |
#include <vector> | |
typedef std::vector<int> vec_type; | |
int main() | |
{ | |
static_assert(std::is_same<value_type_or_identity<vec_type>::type, | |
vec_type::value_type>::value, ""); | |
static_assert(std::is_same<value_type_or_identity<int>::type, | |
int>::value, ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment