Last active
November 3, 2015 02:18
-
-
Save chenfengyuan/75122c77a94497da107c 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
#include <iostream> | |
#if defined(__clang__) | |
enum { max_depth = 256}; | |
#else | |
enum { max_depth = 100}; | |
#endif | |
#define INIT_REGISTER(REG) \ | |
template<typename T, int N> \ | |
struct REG:REG<T, N-1>{ \ | |
enum {value = N}; \ | |
}; \ | |
template<typename T> \ | |
struct REG<T, 0>{ \ | |
enum {value = 0}; \ | |
}; \ | |
template <typename T> \ | |
REG<T, 0> get(REG<T, 0>){ \ | |
return {}; \ | |
} | |
#define GET_REGISTERED_TIMES(REG, T) \ | |
decltype(get(REG<T, max_depth>()))::value | |
#define REGISTER(REG, T) \ | |
REG<T, GET_REGISTERED_TIMES(REG, T) + 1> get(REG<T, GET_REGISTERED_TIMES(REG, T) + 1>){ \ | |
return {}; \ | |
}\ | |
static_assert(true, "") | |
#define IF_REGISTED(REG, T) \ | |
(GET_REGISTERED_TIMES(REG, T) > 0) | |
using namespace std; | |
INIT_REGISTER(Int); | |
REGISTER(Int, int); | |
REGISTER(Int, int); | |
INIT_REGISTER(Float); | |
REGISTER(Float, float); | |
REGISTER(Float, float); | |
REGISTER(Float, double); | |
REGISTER(Float, double); | |
REGISTER(Float, double); | |
int main() | |
{ | |
cout << "Int:int times: " << GET_REGISTERED_TIMES(Int, int) << "\n"; | |
cout << "if int is Int: " << IF_REGISTED(Int, int) << "\n"; | |
cout << "if long is Int: " << IF_REGISTED(Int, long) << "\n"; | |
cout << "if float is Int: " << IF_REGISTED(Int, float) << "\n\n"; | |
cout << "Float:double times: " << GET_REGISTERED_TIMES(Float, double) << "\n"; | |
cout << "if int is Float: " << IF_REGISTED(Float, int) << "\n"; | |
cout << "if double is Float: " << IF_REGISTED(Float, double) << "\n"; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Int:int times: 2
if int is Int: 1
if long is Int: 0
if float is Int: 0
Float:double times: 3
if int is Float: 0
if double is Float: 1