C++ Templates
C++ Templates
Templates are a way to allow functions and classes to use the same code for many different data types.
To declare a template you use the template
keyword followed by a list of
template parameters in angle brackets:
template <parameter1, parameter2, parameter3>
Immediately after the template declaration, you write a function or class definition the normal way. The function or class will have access to special data types and values given by the template declaration.
Example
Use a template to create a class that can use any data type:
template <class MyType>
class MyClass {
public:
MyType first;
MyType second;
MyClass(MyType f, MyType s) {
first = f;
second = s;
}
};
int main() {
// Use MyClass with strings
MyClass<string> stringObj( "Hello", "World" );
cout << stringObj.first << " " << stringObj.second << "\n";
// Use MyClass with integers
MyClass<int> intObj( 4, 5 );
cout << intObj.first << " " << intObj.second << "\n";
return 0;
}
Try it Yourself »
Example explained
- The first line
template <class MyType>
declares the template class MyType
is a template parameter that makes MyType possible to represent any data type. You will learn about template parameters in the next chapter.MyClass<string> stringObj( "Hello", "World" );
creates aMyClass
object whereMyType
meansstring
MyClass<int> stringObj( 4, 5 );
creates aMyClass
object whereMyType
meansint
C++ Template Parameters
Template parameters are how you specify how many data types and values a template has access to.
There are two kinds of template parameters: typed parameters and non-typed parameters.
Typed Parameters
Typed parameters allow you to specify a "wildcard" data type for a class or function.
A typed parameter uses the keyword class
or typename
followed by a name.
There is no difference between the class
and typename
keywords, you can use any one of them.
The syntax of a template with typed parameters looks like this:
template <class A, class B>
The following example declares two different typed parameters:
Example
template <class A, class B>
class MyClass {
public:
A first;
B second;
MyClass(A f, B s) {
first = f;
second = s;
}
};
int main() {
MyClass<int,string> myObj( 15, "Some Text" );
cout << myObj.first << " " << myObj.second << "\n";
return 0;
}
Try it Yourself »
Example explained
- The line
template <class A, class B>
indicates that there are two different data types which are assigned the namesA
andB
A first;
creates an attribute namedfirst
of typeA
B second;
creates an attribute namedsecond
of typeB
- The constructor
MyClass(A f, B s)
has two parameters:f
is of typeA
ands
is of typeB
-
The line
MyClass<int,string> myObj( 15, "Some Text" );
creates aMyClass
object whereA
meansint
andB
meansstring
.
Non-Typed Parameters
Non-typed parameters let you pass values directly to the class or function that is using the template.
To add a non-typed parameter, write the data type of the value followed by a name. The syntax looks like this:
template <dataType1 name1, dataType2 name2>
This example declares a class with a non-typed parameter:
Example
template <int N>
class MyClass {
public:
string myStrings[N];
};
int main() {
MyClass<2> myObj;
myObj.myStrings[0] = "Hello";
myObj.myStrings[1] = "World";
cout << myObj.myStrings[0] << " ";
cout << myObj.myStrings[1] << "\n";
return 0;
}
Try it Yourself »
Example explained
-
The line
template <int N>
indicates that the parameter N contains an integer. -
string myStrings[N];
uses the value ofN
to set the size of an array of strings. -
MyClass<2> myObj;
creates aMyClass
object and sets the value ofN
to 2, which results in themyStrings
array having 2 strings.
Another Example
This example illustrates how to use both typed and non-typed parameters in a template class:
Example
template <class T, int N>
class MyClass {
public:
T myArray[N];
};
int main() {
// Using string data type and 2 elements
MyClass<string, 2> stringObj;
stringObj.myArray[0] = "Hello";
stringObj.myArray[1] = "World";
cout << stringObj.myArray[0] << " ";
cout << stringObj.myArray[1] << "\n";
// Using int data type and 4 elements
MyClass<int, 4> intObj;
intObj.myArray[0] = 2;
intObj.myArray[1] = 4;
intObj.myArray[2] = 6;
intObj.myArray[3] = 8;
cout << intObj.myArray[0] << " ";
cout << intObj.myArray[1] << " ";
cout << intObj.myArray[2] << " ";
cout << intObj.myArray[3] << "\n";
return 0;
}
Try it Yourself »