Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Template based programming in Visual C++ (http://www.go4expert.com/articles/template-based-programming-visual-cpp-t3525/)

Sanskruti 21Mar2007 19:02

Template based programming in Visual C++
Templates, which are sometimes called parameterized types, are mechanisms for generating functions and classes based on type parameters. By using templates, you can design a single class or function that operates on data of many types, instead of having to create a separate class for each type.

For example, to create a typesafe function that returns the minimum of two parameters without using templates, you would write a set of overloaded functions like this:

Code: CPP

int min( int x, int y )
    return ( x <y ) ? x : y;

// min for longs
long min( long x, long y )
    return ( x < y ) ? x : y;

// min for chars
char min( char x, char y )
    return ( x < y ) ? x : y;

By using templates, you can reduce this duplication to a single function template:

Code: CPP

template <class T> T min( T x, T y )
    return ( x < y ) ? x : y;

Templates can significantly reduce source code size and increase code flexibility without reducing type safety.

There are two types of templates:

1. Function templates

2. Class templates.

In the previous example, min is a function template.

A class template is a class with a parameter, such as:

Code: CPP

<class T> class A {
T m_t;
      A(T t): m_t(t) {}
void f(T t);
int main()
    A<int> a(10);

Templates are declared and defined somewhat like other functions and classes, with some major differences.

A template declaration does not fully define a function or class; it only defines a syntactical skeleton for a class or function.

An actual class or function is created from a template by a process called instantiation. The individual classes or functions created are referred to as instantiated.

Instantiation of classes or functions can be done explicitly or implicitly. Explicit instantiation is a way of calling out in code what versions of the template are to be generated. Implicit instantiation allows templates to be instantiated as needed at the point where they are first used.

Templates can also be parameterized by a value parameter, in which case the template parameter is declared like the parameter to a function. Floating-point types and class types are not allowed as value parameters.

A common problem with templates is that they can be a one-size-fits-all solution, meaning that the same code applies to all types. If you need to customize the behavior of the template for a particular type, then you can use specialization. Using explicit specialization, a template can be specialized for a particular real type, not a generic type. A class template can also be partially specialized, which is useful if you have a template with multiple type parameters and you only want to customize the behavior with respect to some but not all parameters.

A partial specialization is still generic and needs real template arguments to produce an actual instantiated class.

You can instantiate a class template much like you would instantiate a normal class, but you must include the template arguments within angle brackets (<>). These template arguments can be any type if the template argument list contains the class or typename keyword, or a value of the appropriate type if the argument is a non-type argument. No special syntax is required to call a function template, although the angle brackets and template arguments can be required if the template parameters cannot be deduced from the arguments to the function.

Code: CPP

template< class T, int b >  class MyStacků.

In this case, the template can receive a type (class T) and a constant parameter (int b). The template will use type T and the constant integer b upon instantiation. A template declaration itself does not generate code; it specifies a family of classes or functions, one or more of which will be generated when referenced by other code.

Template declarations have global, namespace, or class scope. They cannot be declared within a function.

Non-type template parameters must be of integral, enumeration, pointer, reference, or pointer to member type, and must be constant at compile time. They can be qualified as const or volatile types. Floating point values are not allowed as template parameters. Objects of class, struct or union type are not allowed as non-type template parameters, although pointers to such objects are allowed. Arrays passed as non-type template parameters are converted into pointers. Functions passed as non-type parameters are treated as function pointers. String literals are not allowed as template parameters.

The typename keyword can be used in the template parameter list.

The following template declarations are identical:

Code: CPP

template< class A, class B > class X...
template< typename A, typename B > class X...

parvez.yu 6Mar2008 14:58

Re: Template based programming in Visual c++
a good description

All times are GMT +5.5. The time now is 04:07.