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:
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:
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.
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:
Re: Template based programming in Visual c++
a good description
|All times are GMT +5.5. The time now is 00:33.|