本文共 4438 字,大约阅读时间需要 14 分钟。
#include <iostream> #include <cstring> const int MAXLEN_TYPE_DESCRIPTOR = 200; template <typename T> struct type_descriptor { operator const char*() { return name_; } static const char* name_; }; template <typename T> const char* type_descriptor<T>::name_ = "Unkown type"; template <> const char* type_descriptor<char>::name_ = "char"; template <> const char* type_descriptor<short>::name_ = "short"; template <> const char* type_descriptor<int>::name_ = "int"; template <> const char* type_descriptor<long>::name_ = "long"; template <> const char* type_descriptor<long long>::name_ = "long long"; template <> const char* type_descriptor<unsigned char>::name_ = "unsigned char"; template <> const char* type_descriptor<unsigned short>::name_ = "unsigned short"; template <> const char* type_descriptor<unsigned int>::name_ = "unsigned int"; template <> const char* type_descriptor<unsigned long>::name_ = "unsigned long"; template <> const char* type_descriptor<unsigned long long>::name_ = "unsigned long long"; template <> const char* type_descriptor<float>::name_ = "float"; template <> const char* type_descriptor<double>::name_ = "double"; template <> const char* type_descriptor<long double>::name_ = "long double"; template <> const char* type_descriptor<void>::name_ = "void"; template <> const char* type_descriptor<bool>::name_ = "bool"; |
template <typename T> struct type_descriptor<T*> { operator const char*() { static char name[MAXLEN_TYPE_DESCRIPTOR]; strcpy(name, "pointer to "); strcat(name, type_descriptor<T>()); return name; } }; template <typename T> struct type_descriptor<T&> { operator const char*() { static char name[MAXLEN_TYPE_DESCRIPTOR]; strcpy(name, "reference to "); strcat(name, type_descriptor<T>()); return name; } }; template <typename T> struct type_descriptor<T const> { operator const char*() { static char name[MAXLEN_TYPE_DESCRIPTOR]; strcpy(name, type_descriptor<T>()); strcat(name, " const"); return name; } }; template <typename T> struct type_descriptor<T volatile> { operator const char*() { static char name[MAXLEN_TYPE_DESCRIPTOR]; strcpy(name, type_descriptor<T>()); strcat(name, " volatile"); return name; } }; template <typename T> struct type_descriptor<T[]> { operator const char*() { static char name[MAXLEN_TYPE_DESCRIPTOR]; strcpy(name, "array of "); strcat(name, type_descriptor<T>()); return name; } }; template <typename R> struct type_descriptor<R (*)()> { operator const char*() { static char name[MAXLEN_TYPE_DESCRIPTOR]; strcpy(name, "pointer to function returning "); strcat(name, type_descriptor<R>()); return name; } }; template <typename R, typename A> struct type_descriptor<R (*)(A)> { operator const char*() { static char name[MAXLEN_TYPE_DESCRIPTOR]; strcpy(name, "pointer to function with "); strcat(name, type_descriptor<A>()); strcat(name, " returning "); strcat(name, type_descriptor<R>()); return name; } }; |
int main() { std::cout << type_descriptor<int>() << std::endl; std::cout << type_descriptor<char*>() << std::endl; std::cout << type_descriptor<short const*&>() << std::endl; std::cout << type_descriptor<long const* volatile>() << std::endl; std::cout << type_descriptor<char *(*[])()>() << std::endl; std::cout << type_descriptor<char *(*[])(int&)>() << std::endl; return 0; } Output: int pointer to char reference to pointer to short const pointer to long const volatile array of pointer to function returning pointer to char array of pointer to function with reference to int returning pointer to char |
转载地址:http://okxzn.baihongyu.com/