Template based programming in Visual C++

Discussion in 'C++' started by Sanskruti, Mar 21, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    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:
    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:
    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:
    <class T> class A { 
    T m_t;
     public: 
          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:
    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:
    template< class A, class B > class X... 
    template< typename A, typename B > class X...
     
  2. parvez.yu

    parvez.yu New Member

    Joined:
    Feb 14, 2008
    Messages:
    100
    Likes Received:
    0
    Trophy Points:
    0
    a good description
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice