Created
July 30, 2015 10:43
-
-
Save paosidufygthrj/9110b91bcbcbd94567ef to your computer and use it in GitHub Desktop.
[C++]T::value か規定値を返すメタ関数
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 <iostream> | |
#include <type_traits> | |
using namespace std; | |
template <typename T, typename Ignore = void> | |
struct value_getter { | |
static const int value = 0; | |
}; | |
template <typename T> | |
struct value_getter<T, typename enable_if<(T::value != 0)>::type> { | |
static const decltype(T::value) value = T::value; | |
}; | |
struct A { static const int value = 10; }; | |
struct B { enum { value = 1 }; }; | |
struct C {}; | |
int main() { | |
cout << value_getter<A>::value << endl; // 10 | |
cout << value_getter<B>::value << endl; // 1 | |
cout << value_getter<C>::value << endl; // 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment