Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you find templates useful?

At my workplace, we tend to use iostream, string, vector, map, and the odd algorithm or two. We haven't actually found many situations where template techniques were a best solution to a problem.

What I am looking for here are ideas, and optionally sample code that shows how you used a template technique to create a new solution to a problem that you encountered in real life.

As a bribe, expect an up vote for your answer.

like image 822
EvilTeach Avatar asked Oct 26 '08 00:10

EvilTeach


People also ask

Where are templates used?

Templates are pre-formatted documents, intended to speed up the creation of commonly used document types such as letters, fax forms, or envelopes. Templates are also used as guidelines for creating documents in a specific format (for example, the required format for submitting a paper to a scientific journal).

Where are templates found?

By default, user templates files are stored in the following location: In Windows XP: \Documents and Settings\<user name>\Application Data\Microsoft\Templates. In Windows Vista or Windows 7: \Users\<user name>\AppData\Roaming\Microsoft\Templates.

How are templates useful to us?

Templates simplify the creation of documents. Templates can ease our workload and make us feel less stressed, and, at the same time, they increase efficiency. Templates increase the attention of the audience. They help in saving time and money.


1 Answers

General info on templates:

Templates are useful anytime you need to use the same code but operating on different data types, where the types are known at compile time. And also when you have any kind of container object.

A very common usage is for just about every type of data structure. For example: Singly linked lists, doubly linked lists, trees, tries, hashtables, ...

Another very common usage is for sorting algorithms.

One of the main advantages of using templates is that you can remove code duplication. Code duplication is one of the biggest things you should avoid when programming.

You could implement a function Max as both a macro or a template, but the template implementation would be type safe and therefore better.

And now onto the cool stuff:

Also see template metaprogramming, which is a way of pre-evaluating code at compile-time rather than at run-time. Template metaprogramming has only immutable variables, and therefore its variables cannot change. Because of this template metaprogramming can be seen as a type of functional programming.

Check out this example of template metaprogramming from Wikipedia. It shows how templates can be used to execute code at compile time. Therefore at runtime you have a pre-calculated constant.

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

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

// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
    int x = Factorial<4>::value; // == 24
    int y = Factorial<0>::value; // == 1
}
like image 151
Brian R. Bondy Avatar answered Sep 27 '22 20:09

Brian R. Bondy