Re: template specialization for multiple classes
On Jun 19, 7:27 pm, micon...@gmail.com wrote:
Is it possible to declare a template specialization in c++ that fits
multiple classes?
Yes, by using type_traits to group related template type parameters.
For example, I have a class that needs a specialization for 4
different "types" of type. I need one specialization for
unsigned integer types (long, short, int), one for signed integer
types, one for signed floating point types (float, double), and one
for unsigned.
Can I get away with only 4 template specializations? Or do I need to
duplicate the code for every type?
No, there are several ways that one could consolidate related types.
For example, the program could declare four base classes (Integer,
UnsignedInteger, Float, and UnsignedFloat) and then have the class
template derive from the appropriate base class depending on the type
of its template parameter:
#include <tr1/type_traits> // or <boost/type_traits>
// Declare four base classes to represent each of
// the four number types
struct Integer {};
struct UnsignedInteger {};
struct Float {};
struct UnsignedFloat {}; // does not exist in C++
// Declare a NumberTypeHelper class template that selects
// one of the above classes - depending on the properties
// of the arithmetic type provided
template < bool IsArithmetic, // compile-time error, if false
bool isIntegral,
bool isSigned>
struct NumberTypeHelper; // Note: declared, not defined
// Specialize NumberTypeHelper for the each of the four supported
// integral/float and signed/unsigned combinations
template <>
struct NumberTypeHelper<true, true, true>
{
typedef Integer type;
};
template <>
struct NumberTypeHelper<true, true, false>
{
typedef UnsignedInteger type;
};
template <>
struct NumberTypeHelper<true, false, true>
{
typedef Float type;
};
template <>
struct NumberTypeHelper<true, false, false>
{
typedef UnsignedFloat type;
};
// Now have the Number class template use the NumberTypeHelper helper
// class template to select the appropriate base class for the
// template type parameter provided.
using std::tr1::is_arithmetic;
using std::tr1::is_integral;
using std::tr1::is_signed;
template <class T>
struct Number : public NumberTypeHelper< is_arithmetic<T>::value,
is_integral<T>::value,
is_signed<T>::value >::type
{
// ...
};
int main()
{
}
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]