C++ Templates - Introduction


Before diving deeper into the C++ templates, let us quickly review the various programming paradigms and see where C++ template programming falls in. Following are three most common programming paradigms (in context of C++ language):
1.     Procedural programming
2.     Object-oriented programming (OOP)
3.     Generic programming
In procedural way of programming, program is written as procedure or steps to obtain some desired result. The program written in C is most a common example of procedural programming - one defines the number of methods and calls them as required.
The object-oriented language like C++ allows the creation of user defined data structures that are not just consisting of data-fields but also the methods. This means, an instance of data-structure, typically called object, offers data and methods/algorithms to access/manipulate/manage the data. The object-oriented programming are based on concepts of polymorphism, encapsulation, inheritance, late binding etc and this requires different programming mind-set than for procedural programming.
In generic programming, data-structures/algorithms are written in terms of to-be-specified-later types. As needed, generic code is instantiated for specific type by passing the same as arguments. In C++, templates provide a way to write generic programs. Again, generic programming require different mind-set than for above two programming paradigm.
Where C++ Templates can help me?
In day-to-day programming, you might have encountered the situation where you are writing more-or-less similar algorithms for different types of data. For example, you have implemented a list data-structure that works only for double data-type and now you need to implement similar data-structure for another data-type, say for the objects of your favorite class. Here, the functionality that these two data-structures provide is very similar but it is for two different data-types. In such kinds of situation, C++ templates could be a solution. C++ templates allow you to write code that is abstracted from their data types. Meaning, you implement the whole algorithm without knowing the actual data-type and provide the actual data-type only at the time of usage (i.e. instantiation) as an argument to your template. You just need to focus on the algorithms and the job of required code generation will be done by the compiler. This way you can write a type-transparent, but still type-safe, code.
Terminology
Template parameter is names inside angular brackets i.e. <> after the template keyword.
Template argument is names inside angular brackets in a specialization.
Template specialization is what results when you give a template some arguments.
Template instantiation is what results; the compiler generates required code, when you give a template complete set of template arguments.
An instantiation is always a specialization. A specialization may or may not be an instantiation.



Template types
There are two types of templates:
1.     Class Templates
2.     Function Templates
Template parameters
Three types of template parameters
1.     Type parameter
2.     Non-type parameter
3.     Templates
A class/function template definition looks like a regular class/function except that it is prefixed by the keyword template. Following code depicts class templates with type parameter, non-type parameter and template parameter. MyFirstTemplateClass accepts any data-type like int, float, double, user-defined data-type etc as a template argument. MySecondTemplateClass accepts non-type values like int, size_t as a template argument. MyThirdTemplateClass has two parameters and parameter T is a template type parameter. Minimum is a function template.



template<typename T> // Note: T is a type parameter
class MyFirstTemplateClass
{
public:
    typedef T Type;

    MyFirstTemplateClass(T value)
        : _data(value)
    {}
private:
    T _data;
};





template<int N> // Note: N is a non-type (e.g. integral) parameter
class MySecondTemplateClass
{
public:

    MySecondTemplateClass()
    {}

private:
    int _dataArray[N];
};





template<typename O, template<typename O> class T> // Note: T is a
class MyThirdTemplateClass                         // template-type
{
public:
    typedef T ParameterType;
    typedef typename T::Type OType; // Note: keyword typename

    MyThirdTemplateClass(ParameterType& obj)
        : _data(obj)
    {}

private:
    ParameterType _data;
};





// Minimum – function template.
template<typename T>
const T& Minimum(const T& a, const T& b)
{
    return (a < b) ? a : b;
}





C++ Templates - Introduction C++ Templates - Introduction Reviewed by Sourabh Soni on Friday, September 25, 2009 Rating: 5

No comments

Author Details

Image Link [https://3.bp.blogspot.com/-zo21XIdyPqc/VuTrFfUyPhI/AAAAAAAAAO8/EEWTN73XHUA7aTIjuxuBSN-WGaGkNUymA/s1600/sourabhdots3.jpg] Author Name [Sourabh Soni] Author Description [Technocrat, Problem Solver, Corporate Entrepreneur, Adventure Enthusiast] Facebook Username [sourabh.soni.587] Twitter Username [sourabhs271] GPlus Username [#] Pinterest Username [#] Instagram Username [#] LinkedIn Username [sonisourabh] Youtube Username [sonisourabh] NatGeo Username [271730]