DekGenius.com
Team LiB   Previous Section   Next Section

7.9 Tricks with Templates

Template syntax permits recursion, selection, and computation. In other words, it is a full-fledged programming language, albeit a language that is hard to write and even harder to read. The full scope and power of programming with templates is beyond the scope of this book. This section presents some tips for starting your own exploration of this exciting field. Appendix B tells you about some interesting projects in this area.

Suppose you want to write a function to round off floating-point values to a fixed number of decimal places. You decide to write the function by multiplying by a power of 10, rounding off to an integer, and dividing by the same power of 10. You can hardcode the amount, (e.g., 100) in the routine, but you prefer to use a template, in which a template parameter specifies the number of decimal digits to retain. To avoid computing the same power of 10 every time the function is called, you decide to use template programming to compute the constant at compile time.

The ipower<> template in Example 7-14 uses recursion to compute the power of any integer raised to any nonnegative integer value. Three base cases for the recursion are defined as specializations: raising any value to the 0th power is always 1, raising 0 to any power is always 0, and raising 0 to the 0th power is undefined. (As an exercise, try to define ipower more efficiently.) Finally, the ipower<> class template is used to define the round<> function template.

Example 7-14. Computing at compile time
template<int x, unsigned y>
struct ipower {
  enum { value = x * ipower<x, y-1>::value };
};
template<int x>
struct ipower<x, 0> {
  enum { value = 1 };
};
template<unsigned y>
struct ipower<0, y> {
  enum { value = 0 };
};
template<> struct ipower<0, 0> {};

// Round off a floating-point value to a fixed number of digits.
template<unsigned N, typename T>
T round(T x)
{
  if (x < 0.0)
    return std::floor(x * ipower<10,N>::value - 0.5) /
           ipower<10,N>::value;
  else
    return std::floor(x * ipower<10,N>::value + 0.5) /
           ipower<10,N>::value;
}

In addition to compile-time computation, you can write more complicated programs that are evaluated at compile time. For example, the Boost project (described in Appendix B) uses templates to create type lists, that is, lists of types that are manipulated at compile time. Such lists can greatly simplify certain programming tasks, such as implementing type traits. Example 7-15 shows a simplified version of type lists. A list is defined recursively as an empty list or a node that contains a type (head) and a list (tail). (This definition should be familiar to anyone with experience using functional programming languages.)

Example 7-15. Defining type lists
struct empty {};
template<typename H, typename T>
struct node {
  typedef H head;
  typedef T tail;
};

template<typename T1  = empty, typename T2  = empty,
         typename T3  = empty, typename T4  = empty,
         typename T5  = empty, typename T6  = empty,
         typename T7  = empty, typename T8 = empty,
         typename T9  = empty, typename T10 = empty,
         typename T11 = empty, typename T12 = empty
>
struct list {
  typedef node<T1, node<T2, node<T3, node<T4,
          node<T5, node<T6, node<T7, node<T8,
          node<T9, node<T10, node<T11, node<T12,
          empty
          > > > > > > > > > > > > type;
};

template<typename L>
struct length {
  enum { value = 1 + length<typename L::tail>::value };
};
template<>
struct length<empty> {
  enum { value = 0 };
};

template<typename L>
struct is_empty {
  enum { value = false };
};
template<>
struct is_empty<empty> {
  enum { value = true };
};

Actions on type lists are inherently recursive. Thus, to count the number of items in a type list, count the head as one, and add the length of the tail. The recursion stops at the end of the list, which is implemented as a specialization of the length<> template for the empty type.

An important action when using type lists is to test membership. To do this, you must be able to compare two types to see if they are the same. The is_same_type class template uses partial specialization to determine when two types are the same. If the two types specified as template arguments are the same, the specialization sets value to true. If the arguments are different, the primary template is instantiated, and value is false. Example 7-16 shows is_same_type and how it is used in the is_member template.

Example 7-16. Testing membership in a type list
template<typename T, typename U>
struct is_same_type {
  enum  { value = false };
};
template<typename T>
struct is_same_type<T, T> {
  enum { value = true };
};

template<typename T, typename L>
struct is_member {
  enum { value = is_same_type<T, typename L::head>::value
              || is_member<T, typename L::tail>::value };
};

template<typename T>
struct is_member<T, empty> {
  enum { value = false };
};

Once you can define type lists and test whether a type is in a type list, you can use these templates to implement simple type traits. For example, you can create a list of the integral types and test whether a type is one of the fundamental integral types. (Testing for integral types is important when implementing standard containers, as discussed in Chapter 10.) Example 7-17 shows some simple uses of type lists.

Example 7-17. Using type lists
#include <iostream>
#include <ostream>

typedef list<bool, char, unsigned char, signed char,
             int, short, long, unsigned,
             unsigned long, unsigned short>::type int_types;
typedef list<float, double, long double>::type real_types;

int main(  )
{
  using namespace std;
  cout << is_same_type<int,int>::value << '\n';
  cout << is_same_type<int, signed int>::value << '\n';
  cout << is_same_type<int, unsigned int>::value << '\n';
  cout << is_member<int, int_types>::value << '\n';
  cout << is_member<float, int_types>::value << '\n';
  cout << is_member<ostream, int_types>::value << '\n';
}
    Team LiB   Previous Section   Next Section