The attached document was submitted for publication to C/C++ Users Journal
today, but I thought it might be worth submitting to Boost as well.
Perhaps my spelling class template could be folded into type_traits?
Robert Allan Schwartz
Have you ever wished you could convert a template parameter to a std::std::string,
in something like the following:
template <typename T>
void foo(T)
{
cout << "T is " << /* convert T to std::string */ << endl;
}
Now you can!
First, create a primary template:
#include <string>
// primary template:
template <typename T>
class spelling;
Second, create partial specializations for pointers and references to T:
// partial specialization for T *:
template <typename T>
class spelling<T *>
{
public:
static const std::string result;
};
template <typename T>
const std::string spelling<T *>::result = spelling<T>::result + " *";
// partial specialization for T * const:
template <typename T>
class spelling<T * const>
{
public:
static const std::string result;
};
template <typename T>
const std::string spelling<T * const>::result = spelling<T>::result + " *
const";
// partial specialization for T const *:
template <typename T>
class spelling<T const *>
{
public:
static const std::string result;
};
template <typename T>
const std::string spelling<T const *>::result = spelling<T>::result + " const
*";
// partial specialization for T const * const:
template <typename T>
class spelling<T const * const>
{
public:
static const std::string result;
};
template <typename T>
const std::string spelling<T const * const>::result = spelling<T>::result + "
const * const";
// partial specialization for T &:
template <typename T>
class spelling<T &>
{
public:
static const std::string result;
};
template <typename T>
const std::string spelling<T &>::result = spelling<T>::result + " &";
// partial specialization for T const &:
template <typename T>
class spelling<T const &>
{
public:
static const std::string result;
};
template <typename T>
const std::string spelling<T const &>::result = spelling<T>::result + " const
&";
Third, create partial specializations for all the primitive types:
// full specialization for unsigned char:
template <>
class spelling<unsigned char>
{
public:
static const std::string result;
};
const std::string spelling<unsigned char>::result = "unsigned char";
// full specialization for unsigned short:
template <>
class spelling<unsigned short>
{
public:
static const std::string result;
};
const std::string spelling<unsigned short>::result = "unsigned short";
// full specialization for unsigned int:
template <>
class spelling<unsigned int>
{
public:
static const std::string result;
};
const std::string spelling<unsigned int>::result = "unsigned int";
// full specialization for unsigned long:
template <>
class spelling<unsigned long>
{
public:
static const std::string result;
};
const std::string spelling<unsigned long>::result = "unsigned long";
// full specialization for signed char:
template <>
class spelling<signed char>
{
public:
static const std::string result;
};
const std::string spelling<signed char>::result = "signed char";
// full specialization for signed short:
template <>
class spelling<signed short>
{
public:
static const std::string result;
};
const std::string spelling<signed short>::result = "signed short";
// full specialization for signed int:
template <>
class spelling<signed int>
{
public:
static const std::string result;
};
const std::string spelling<signed int>::result = "signed int";
// full specialization for signed long:
template <>
class spelling<signed long>
{
public:
static const std::string result;
};
const std::string spelling<signed long>::result = "signed long";
// full specialization for bool:
template <>
class spelling<bool>
{
public:
static const std::string result;
};
const std::string spelling<bool>::result = "bool";
// full specialization for char:
template <>
class spelling<char>
{
public:
static const std::string result;
};
const std::string spelling<char>::result = "char";
// full specialization for wchar_t:
template <>
class spelling<wchar_t>
{
public:
static const std::string result;
};
const std::string spelling<wchar_t>::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<my_enum>
{
public:
static const std::string result;
};
const std::string spelling<my_enum>::result = "enum my_enum";
// full specialization for my_struct:
template <>
class spelling<my_struct>
{
public:
static const std::string result;
};
const std::string spelling<my_struct>::result = "struct my_struct";
// full specialization for my_class:
template <>
class spelling<my_class>
{
public:
static const std::string result;
};
const std::string spelling<my_class>::result = "class my_class";
// full specialization for my_union:
template <>
class spelling<my_union>
{
public:
static const std::string result;
};
const std::string spelling<my_union>::result = "union my_union";
Last, but not least, use the spelling class template to convert T to a std::string:
template <typename T>
void foo(T)
{
cout << "T is " << spelling<T>::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.
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost