Have you ever wished you could convert a template parameter to a std::std::string, in something like the following: template void foo(T) { cout << "T is " << /* convert T to std::string */ << endl; } Now you can! First, create a primary template: #include // primary template: template class spelling; Second, create partial specializations for pointers and references to T: // partial specialization for T *: template class spelling { public: static const std::string result; }; template const std::string spelling::result = spelling::result + " *"; // partial specialization for T * const: template class spelling { public: static const std::string result; }; template const std::string spelling::result = spelling::result + " * const"; // partial specialization for T const *: template class spelling { public: static const std::string result; }; template const std::string spelling::result = spelling::result + " const *"; // partial specialization for T const * const: template class spelling { public: static const std::string result; }; template const std::string spelling::result = spelling::result + " const * const"; // partial specialization for T &: template class spelling { public: static const std::string result; }; template const std::string spelling::result = spelling::result + " &"; // partial specialization for T const &: template class spelling { public: static const std::string result; }; template const std::string spelling::result = spelling::result + " const &"; Third, create partial specializations for all the primitive types: // full specialization for unsigned char: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "unsigned char"; // full specialization for unsigned short: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "unsigned short"; // full specialization for unsigned int: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "unsigned int"; // full specialization for unsigned long: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "unsigned long"; // full specialization for signed char: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "signed char"; // full specialization for signed short: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "signed short"; // full specialization for signed int: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "signed int"; // full specialization for signed long: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "signed long"; // full specialization for bool: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "bool"; // full specialization for char: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "char"; // full specialization for wchar_t: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "wchar_t"; Fourth, create your own types (enums, structs, classes, unions): // add your types here: enum my_enum { ENUM1, ENUM2, ENUM3 }; struct my_struct { }; class my_class { }; union my_union { }; Fifth, create full specializations for your own types: // full specialization for my_enum: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "enum my_enum"; // full specialization for my_struct: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "struct my_struct"; // full specialization for my_class: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "class my_class"; // full specialization for my_union: template <> class spelling { public: static const std::string result; }; const std::string spelling::result = "union my_union"; Last, but not least, use the spelling class template to convert T to a std::string: template void foo(T) { cout << "T is " << spelling::result << endl; } int main() { foo(3); foo(ENUM1); foo(my_struct()); foo(my_class()); foo(my_union()); foo(new int); return 0; } Voila! Copyright © 2003 Robert Allan Schwartz. All rights reserved.