Do I Have To Use Typename In A Template
A template is a simple and yet very powerful tool in C++. The elementary idea is to laissez passer data type as a parameter so that we don't need to write the aforementioned lawmaking for dissimilar information types. For example, a software visitor may need sort() for different data types. Rather than writing and maintaining the multiple codes, we can write i sort() and laissez passer data type every bit a parameter.
C++ adds two new keywords to support templates: 'template' and 'typename'. The second keyword tin can always be replaced by keyword 'course'.
How do templates work?
Templates are expanded at compiler time. This is like macros. The difference is, the compiler does blazon checking before template expansion. The idea is simple, source code contains only function/grade, but compiled code may comprise multiple copies of same function/class.
Part Templates We write a generic part that tin be used for different data types. Examples of role templates are sort(), max(), min(), printArray().
Know more on Generics in C++
CPP
#include <iostream>
using
namespace
std;
template
<
typename
T>
T myMax(T x, T y)
{
return
(ten > y)? 10: y;
}
int
main()
{
cout << myMax<
int
>(3, 7) << endl;
cout << myMax<
double
>(3.0, 7.0) << endl;
cout << myMax<
char
>(
'yard'
,
'e'
) << endl;
return
0;
}
Output:
7 7 thou
Below is the programme to implement Bubble Sort using templates in C++:
CPP
#include <iostream>
using
namespace
std;
template
<
course
T>
void
bubbleSort(T a[],
int
due north) {
for
(
int
i = 0; i < n - 1; i++)
for
(
int
j = due north - 1; i < j; j--)
if
(a[j] < a[j - 1])
swap(a[j], a[j - 1]);
}
int
main() {
int
a[5] = {x, 50, 30, 40, twenty};
int
n =
sizeof
(a) /
sizeof
(a[0]);
bubbleSort<
int
>(a, due north);
cout <<
" Sorted array : "
;
for
(
int
i = 0; i < n; i++)
cout << a[i] <<
" "
;
cout << endl;
return
0;
}
Output
Sorted array : 10 xx 30 forty 50
Output:
Sorted array : 10 xx 30 40 50
Class Templates Similar role templates, grade templates are useful when a course defines something that is independent of the data type. Can be useful for classes like LinkedList, BinaryTree, Stack, Queue, Array, etc.
Following is a simple example of template Array course.
CPP
#include <iostream>
using
namespace
std;
template
<
typename
T>
grade
Array {
private
:
T *ptr;
int
size;
public
:
Assortment(T arr[],
int
s);
void
impress();
};
template
<
typename
T>
Array<T>::Array(T arr[],
int
southward) {
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[v] = {1, 2, 3, 4, v};
Assortment<
int
> a(arr, five);
a.print();
render
0;
}
Output:
1 2 3 four five
Tin there exist more than one arguments to templates?
Yes, like normal parameters, we can pass more than than i data types as arguments to templates. The following instance demonstrates the aforementioned.
CPP
#include<iostream>
using
namespace
std;
template
<
course
T,
class
U>
form
A {
T x;
U y;
public
:
A() { cout<<
"Constructor Called"
<<endl; }
};
int
primary() {
A<
char
,
char
> a;
A<
int
,
double
> b;
render
0;
}
Output
Constructor Chosen Constructor Called
Output:
Constructor Called Constructor Called
Tin nosotros specify default value for template arguments?
Yes, similar normal parameters, we tin specify default arguments to templates. The following example demonstrates the same.
CPP
#include<iostream>
using
namespace
std;
template
<
class
T,
form
U =
char
>
grade
A {
public
:
T x;
U y;
A() { cout<<
"Constructor Called"
<<endl; }
};
int
main() {
A<
char
> a;
return
0;
}
Output:
Constructor Chosen
What is the difference between role overloading and templates?
Both function overloading and templates are examples of polymorphism feature of OOP. Function overloading is used when multiple functions do similar operations, templates are used when multiple functions exercise identical operations.
What happens when in that location is a static member in a template grade/office?
Each instance of a template contains its own static variable. Encounter Templates and Static variables for more details.
What is template specialization?
Template specialization allows us to accept unlike lawmaking for a item data type. See Template Specialization for more details.
Tin we pass nontype parameters to templates?
We tin pass non-blazon arguments to templates. Non-type parameters are mainly used for specifying max or min values or any other abiding value for a detail instance of a template. The important thing to annotation almost non-type parameters is, they must exist const. The compiler must know the value of non-type parameters at compile fourth dimension. Because the compiler needs to create functions/classes for a specified not-type value at compile time. In below program, if we supercede 10000 or 25 with a variable, we go a compiler error. Please see this.
Beneath is a C++ program.
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 < northward; i++)
if
(arr[i] < 1000)
m = arr[i];
render
m;
}
int
main()
{
int
arr1[] = {ten, 20, 15, 12};
int
n1 =
sizeof
(arr1)/
sizeof
(arr1[0]);
char
arr2[] = {one, 2, 3};
int
n2 =
sizeof
(arr2)/
sizeof
(arr2[0]);
cout << arrMin<
int
, 10000>(arr1, n1) << endl;
cout << arrMin<
char
, 256>(arr2, n2);
return
0;
}
Output:
10 1
Here is an example of C++ program to show unlike information types using constructor and template. We will perform few actions
- passing character value past creating an objects in primary() function.
- passing integer value by creating an objects in chief() function.
- passing float value by creating an objects in main() function.
C++
#include <iostream>
#include <conio.h>
template
<
course
Tl>
course
info
{
public
:
info(TI, A)
{
cout<<
"\n"
<<
"A = "
<<A<<
" size of data in bytes:"
<<
sizeof
(ch);
}
};
int
primary()
{
clrscr();
info<
char
>p(
'x'
);
info<
int
>q(22);
info<
float
>r(two.25);
return
0;
}
Output:
A = ten size of information in bytes: i A = 22 size of data in bytes: two A = two.25 size of information in bytes: iv
What is template metaprogramming?
Encounter Template Metaprogramming
Yous may also like to take a quiz on templates.
Coffee also supports these features. Java calls information technology generics .
Please write comments if you lot find anything incorrect, or you want to share more information about the topic discussed above.
Do I Have To Use Typename In A Template,
Source: https://www.geeksforgeeks.org/templates-cpp/
Posted by: rowestrust.blogspot.com
0 Response to "Do I Have To Use Typename In A Template"
Post a Comment