Created
September 22, 2010 10:09
-
-
Save niha/591450 to your computer and use it in GitHub Desktop.
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
template <bool cond, typename _, typename else_> | |
struct enable_if { typedef typename else_::type type; }; | |
template <typename then_, typename _> | |
struct enable_if<true, then_, _>{ typedef typename then_::type type; }; | |
template <int n> | |
struct num { | |
static const int value = n; | |
typedef num<n> type; | |
}; | |
template <int n, int byte> | |
struct num_to_byte_impl { | |
static const bool cond = n > (2 << (8 * byte - 1)) - 1; | |
typedef typename enable_if<cond, | |
num_to_byte_impl<n, byte+1>, | |
num<byte> >::type type; | |
}; | |
template <int n> | |
struct num_to_byte { | |
typedef typename num_to_byte_impl<n, 1>::type type; | |
static const int value = type::value; | |
}; | |
template <int n> | |
struct byte_to_type; | |
template <> struct byte_to_type<1> { typedef char type; }; | |
template <> struct byte_to_type<2> { typedef short type; }; | |
template <> struct byte_to_type<4> { typedef int type; }; | |
template <> struct byte_to_type<8> { typedef long long type; }; | |
template <int n> | |
struct num_to_type { | |
static const int byte = num_to_byte<n>::value; | |
typedef typename byte_to_type<byte>::type; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment