Advanced Programming C++
-
Upload
guestf0562b -
Category
Technology
-
view
8.069 -
download
12
description
Transcript of Advanced Programming C++
Why you should use templates
Templates are type-safe. Based on the fact that the types which the template acts on is already known at compile time, the compiler is able to do the type checking before a error occurs.
Templates are more easy to write.
Why you should use templates (II)
It is possible to optimize the code.
Templates provide direct support for generic programming.
The use of templates is a good possibilitiy to reuse code.
Functions Templates
We will illustrate an example:It is wanted to create a function that independently gave back the minimum between two values of its type (it assumes that both have he himself type).
How can we implement this ?How can we implement this ?
Functions Templates (II)
It would be possible to be thought about to define the function so many times as data types can be displayed...
(int, long, float, double, etc.)
Solution: uses Templates!.
template <class T> T minimum( T a, T b);
Functions Templates
template <class T> T minimum( T a, T b);
In that case with <class T> it is being indicated that one is a group whose parameter is going to be the T type and that as the value of return as each one of both arguments is going to be of this data type T.
Functions Templates
But it can be that it is needed to use more than one data type and some other constant parameter that can be used in the declarations.
template <class T1, class T2> void mixing(T1 a, T2 b);
Functions Templates
Could be the case of that some arguments or the return data is a data type that we know and constant.
template <class T> T minimum(T a, T b){ if(a <= b) return a; else return b;}
The main program#include <iostream.h>template <class T> T minimum(T a, T b);void main(void){ int eOne=1; int eTwo=5; cout << minimum(euno, edos) << endl; long lOne=1; long lTwo=5; cout << minimum(lOne, ltwo) << endl;
char cOne='a'; char cTwo='d'; cout << minimum(cOne, cTwo) << endl; double dOne=1.8; double dTwo=1.9; cout << minimum(dOne, dTwo) << endl;}
Functions Templates
The execution of the previous program demonstrates that the type of the arguments and the value of return of the minimum() function are distinguished in each case to those of the call.
Warning : It is obvious also that an error will take place if two variables of different type go like arguments .
Classes Templates
Similarly with the functions, a parameter will be defined that will indicate the data type with which more ahead the objects will be created
Template Specialization
Any generic code development will need a small case where it needs to do some hard coding or to avoid some amount generic code.
Come in : Template Specialization!
Template Specialization
It is possible to provide different definitions of a template, if you want to handle a specific type in an alternative way.
The right implementation is chosen by the compiler according the template arguments.
It is a very powerful feature! It is often used for optimization.
Template Specialization (II)
The idea of C++ class template specialization is similar to function template overloading.
Example : if we wants to write a Queue class template with the ability to handle all data types
Once the template is specialized, all the member functions should be declared and defined for the specific data type.
With Specialization
Declaration should include the template keyword with the class name followed by the type in the angular brackets.
template<> class MyQueue<double> { std::vector<double> data; public: void Add(double const &); void Remove(); void Print(); };
Without Specialization
template <typename T>class MyQueue{ std::vector<T> data; public: void Add(T const &d); void Remove(); void Print();};
Other Example :
Standard Implementation:
template <class T, int size>void Example::add (int number) {
for (int i=0; i<size; i++) {data[i] += T(number);
}}
Specialized implementation:
template <>void Example<int,4>::add (int number) {data[0] += number;data[1] += number;data[2] += number;data[3] += number;
}
Defining Functions for C++ Class Template Specialization
Now all the functions for this specialized template should be declared individually for the double data type. For example:
template <> void MyQueue<double>::Add(double const &d)
{ data.push_back(d);}
Defining Functions for C++ Class Template
Specialization(II)template <> void MyQueue<double>::Print(){ std::vector <double>::iterator It1; It1 = data.begin();
cout<<"Double"<<endl;
for ( It1 = data.begin( ) ; It1 != data.end( ) ; It1++ )
cout << " " << *It1<<endl;
}
If the above template MyQueue is used for double data type, the C++ compiler will not generate another version of class for the double data type from the generic MyQueue<typename T> template
Instead it will use the one defined as above in MyQueue<double>. For any other data types, the compiler will expand the generic C++ class template and use it.
Template Specialization
It is possible to add a specialization of the template later without changing the code where the template is called.
Specialization types: Full specialization
As described in the previous slide Partial specialization
As described in the next slide
Partial Specification
There are also partial specifications possible. That means that the implementation of the template can still allow parameters.
Example:
template <class T>void Example<T,1>::add (int number) { data[0] += T(number);
}
Partial Specialization
template <typename T>class MyQueue<T,T>{};
The above is the example for partial specialization with the same type.
template <typename T> class MyQueue<T,double>{};
Order of Specializations
There are specializations that are more specialized than another one.
The most specialized implementation will be preferred.
Example:
template<class T> class Vector; //generaltemplate<class T> class Vector<T*>; //specialized for any pointer template<> class Vector<void*>; //specialized for void*