What Would Be The Best Statement About When Does Instantiation Of A Template Parameter Occur
A template is a simple and yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don't need to write the same lawmaking for unlike data types. For example, a software visitor may need sort() for unlike data types. Rather than writing and maintaining the multiple codes, nosotros tin can write 1 sort() and pass data type every bit a parameter.
C++ adds two new keywords to support templates: 'template' and 'typename'. The second keyword can e'er be replaced past keyword 'course'.
How do templates piece of work?
Templates are expanded at compiler time. This is similar macros. The difference is, the compiler does type checking before template expansion. The idea is simple, source lawmaking contains only part/class, but compiled lawmaking may contain multiple copies of same function/class.
Function Templates Nosotros write a generic function that tin can be used for unlike data types. Examples of office templates are sort(), max(), min(), printArray().
Know more on Generics in C++
CPP
#include <iostream>
using namespace std;
template < typename T>
T myMax(T 10, T y)
{
return (x > y)? x: y;
}
int main()
{
cout << myMax< int >(3, 7) << endl;
cout << myMax< double >(three.0, 7.0) << endl;
cout << myMax< char >( 'm' , 'e' ) << endl;
return 0;
}
Output:
7 7 chiliad
Below is the program to implement Bubble Sort using templates in C++:
CPP
#include <iostream>
using namespace std;
template < class T>
void bubbleSort(T a[], int northward) {
for ( int i = 0; i < n - 1; i++)
for ( int j = n - 1; i < j; j--)
if (a[j] < a[j - 1])
bandy(a[j], a[j - one]);
}
int chief() {
int a[five] = {10, 50, 30, 40, 20};
int n = sizeof (a) / sizeof (a[0]);
bubbleSort< int >(a, n);
cout << " Sorted assortment : " ;
for ( int i = 0; i < north; i++)
cout << a[i] << " " ;
cout << endl;
return 0;
}
Output
Sorted array : 10 twenty 30 40 l
Output:
Sorted assortment : 10 xx 30 forty 50
Class Templates Similar office templates, form templates are useful when a course defines something that is independent of the data type. Can be useful for classes similar LinkedList, BinaryTree, Stack, Queue, Assortment, etc.
Following is a simple example of template Assortment class.
CPP
#include <iostream>
using namespace std;
template < typename T>
form Array {
private :
T *ptr;
int size;
public :
Array(T arr[], int s);
void print();
};
template < typename T>
Array<T>::Array(T arr[], int due south) {
ptr = new T[due south];
size = s;
for ( int i = 0; i < size; i++)
ptr[i] = arr[i];
}
template < typename T>
void Array<T>::print() {
for ( int i = 0; i < size; i++)
cout<< " " <<*(ptr + i);
cout<<endl;
}
int main() {
int arr[5] = {1, two, iii, iv, 5};
Assortment< int > a(arr, 5);
a.impress();
render 0;
}
Output:
1 2 3 4 5
Tin at that place be more than i arguments to templates?
Yes, similar normal parameters, we can pass more than than one data types as arguments to templates. The following instance demonstrates the aforementioned.
CPP
#include<iostream>
using namespace std;
template < class T, class U>
class A {
T x;
U y;
public :
A() { cout<< "Constructor Called" <<endl; }
};
int master() {
A< char , char > a;
A< int , double > b;
return 0;
}
Output
Constructor Chosen Constructor Called
Output:
Constructor Chosen Constructor Called
Can we specify default value for template arguments?
Aye, like normal parameters, we can specify default arguments to templates. The following example demonstrates the same.
CPP
#include<iostream>
using namespace std;
template < class T, class U = char >
class A {
public :
T x;
U y;
A() { cout<< "Constructor Called" <<endl; }
};
int main() {
A< char > a;
return 0;
}
Output:
Constructor Called
What is the divergence between function overloading and templates?
Both function overloading and templates are examples of polymorphism feature of OOP. Role overloading is used when multiple functions practice similar operations, templates are used when multiple functions do identical operations.
What happens when there is a static member in a template class/part?
Each instance of a template contains its own static variable. Run across Templates and Static variables for more details.
What is template specialization?
Template specialization allows us to take different code for a detail information blazon. Run into Template Specialization for more details.
Can nosotros pass nontype parameters to templates?
We can pass non-type arguments to templates. Non-type parameters are mainly used for specifying max or min values or any other constant value for a item instance of a template. The of import affair to note about not-type parameters is, they must be const. The compiler must know the value of non-type parameters at compile time. Considering the compiler needs to create functions/classes for a specified non-type value at compile fourth dimension. In below program, if we supersede 10000 or 25 with a variable, we become a compiler error. Please encounter this.
Below is a C++ plan.
CPP
#include <iostream>
using namespace std;
template < class T, int max>
int arrMin(T arr[], int n)
{
int m = max;
for ( int i = 0; i < n; i++)
if (arr[i] < one thousand)
one thousand = arr[i];
return m;
}
int main()
{
int arr1[] = {10, 20, 15, 12};
int n1 = sizeof (arr1)/ sizeof (arr1[0]);
char arr2[] = {1, 2, iii};
int n2 = sizeof (arr2)/ sizeof (arr2[0]);
cout << arrMin< int , 10000>(arr1, n1) << endl;
cout << arrMin< char , 256>(arr2, n2);
render 0;
}
Output:
10 1
Hither is an example of C++ program to show different information types using constructor and template. We volition perform few actions
- passing character value by creating an objects in principal() function.
- passing integer value by creating an objects in chief() office.
- passing float value by creating an objects in main() part.
C++
#include <iostream>
#include <conio.h>
template < course Tl>
form info
{
public :
info(TI, A)
{
cout<< "\n" << "A = " <<A<< " size of data in bytes:" << sizeof (ch);
}
};
int main()
{
clrscr();
info< char >p( 'x' );
info< int >q(22);
info< float >r(ii.25);
return 0;
}
Output:
A = 10 size of data in bytes: 1 A = 22 size of data in bytes: 2 A = 2.25 size of information in bytes: iv
What is template metaprogramming?
See Template Metaprogramming
You may also similar to take a quiz on templates.
Java too supports these features. Java calls it generics .
Please write comments if y'all discover anything incorrect, or you desire to share more data about the topic discussed above.
What Would Be The Best Statement About When Does Instantiation Of A Template Parameter Occur,
Source: https://www.geeksforgeeks.org/templates-cpp/
Posted by: branhamfars1969.blogspot.com

0 Response to "What Would Be The Best Statement About When Does Instantiation Of A Template Parameter Occur"
Post a Comment