Skip to content

Instantly share code, notes, and snippets.

@paosidufygthrj
Created July 30, 2015 10:43
Show Gist options
  • Save paosidufygthrj/9110b91bcbcbd94567ef to your computer and use it in GitHub Desktop.
Save paosidufygthrj/9110b91bcbcbd94567ef to your computer and use it in GitHub Desktop.
[C++]T::value か規定値を返すメタ関数
#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