Science Fair Projects Ideas - Template metaprogramming

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Template metaprogramming

Template metaprogramming is a programming technique to get some evaluations done at compile-time by using the facility of templates in C++. Most of the techniques used in template metaprogramming require a C++ compiler with fairly high Standard Compliance.

What 'programming at compile-time' means is best illustrated with a simple example:

Contents

Factorial with recursion

int factorial(int n) {
  if (n == 1)
    return 1;
  else
    return n * factorial(n - 1);
}

// factorial(4) == (4 * 3 * 2 * 1) == 24

Factorial with template metaprogramming

template <int N>
struct Factorial {
  enum { value = N * Factorial<N - 1>::value };
};

template <>
struct Factorial<1> {
  enum { value = 1 };
};

// Factorial<4>::value == 24

The template metaprogramming solution uses template specialization to provide the ending condition for the recursion. However, the factorial in the latter case, Factorial<4>::value is computed at compile time. This has the natural consequence that Factorial<x>::value can only be used if x is known at compile time, that is if x is a constant or the result of a call to sizeof().

Template metaprogramming does have its practical uses even though its syntax looks clumsy. It may be used to create n-dimensional vector classes where n is known at compile time. The benefit over a more traditional n-dimensional vector is that the loops can be unrolled, resulting in very optimized code.

Consider this example of the addition operator:

Traditional n-dimensional vector class approach

template<int dimension>
Vector<dimension>& Vector<dimension>::operator+=(const Vector<dimension>& rhs) {
  for (int i = 0; i < dimension; ++i) {
    value[i] += rhs.value[i];
  }
  return *this;
}

With template metaprogramming, the loop can be unrolled at compile-time, resulting in code similar to this:

Template metaprogramming enhanced n-dimensional vector class

template<>
Vector<2>& Vector<2>::operator+=(const Vector<2>& rhs) {
  value[0] += rhs.value[0];
  value[1] += rhs.value[1];
  return *this;
}

Note that it results in code like that, but is not programmed like that. But it demonstrates that the loop is gone and so is the loop's overhead. For a real implementation of this technique, see this post on flipcode.com.

Also worth noting is that the programming paradigm used in template metaprogramming is functional and that template metaprogramming in C++ is a Turing-complete technique. The former means that you should be very familiar with recursion if you plan to do template metaprogramming. The latter means that any algorithm can be implemented with template metaprogramming. The only implication is that the algorithm's input values must be known while the code is compiled.

See also

10-26-2009 08:16:03
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice