1 Templates: Int Const Int Const Int Return

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

1 Templates

We have seen that functions can take arguments of specific types and have a specific return type.
We now consider templates, which allow us to work with generic types. Through templates, rather
than repeating function code for each new type we wish to accommodate, we can create functions
that are capable of using the same code for different types. For example:

1 int sum(const int x, const int y) {


2 return x + y;
3 }

For this function to work with doubles, it must be modified to the following:

1 double sum (const double x, const double y) {


2 return x + y;
3 }

For a simple function such as this, it may be a small matter to just make the change as
shown, but if the code were much more complicated, copying the entire function for each new
type can quickly become problematic. To overcome this we rewrite sum as a function template.

The format for declaring a function template is:

template <class identifier> function_declaration;

or

template <typename identifier> function_declaration;

Both forms are equivalent to one another, regardless of what type identifier ends up being.
We can then use identifier to replace all occurrences of the type we wish to generalize.

So, we rewrite our sum function:

1 template <typename T>


2 T sum(const T a, const T b) {
3 return a + b;
4 }

Now, when sum is called, it is called with a particular type, which will replace all Ts in the
code. To invoke a function template, we use:

function_name <type> (parameters);

Here is an example main function using the above sum function template:

1 int main() {
2 cout << sum<int>(1, 2) << endl;
3 cout << Error! Hyperlink reference not valid. 2.43) << endl;
4 return 0;
5 }

This program prints out 3 and 3.64 on separate lines.


The identifier can be used in any way inside the function template, as long as the code
makes sense after identifier is replaced with some type.

It is also possible to invoke a function template without giving an explicit type, in cases
where the generic type identifier is used as the type for a parameter for the function. In the
above example, the following would also have been valid:
1 int main() {
2 cout << sum(1, 2) << endl;
3 cout << sum(1.21, 2.43) << endl;
4 return 0;
5 }

Templates can also specify more than one type parameter. For example:

1 #include <iostream>
2 using namespace std;
3
4 template <typename T, typename U>
5 U sum(const T a, const U b) {
6 return a + b;
7 }
8
9 int main() {
10 cout << sum<int, float>(1, 2.5) << endl;
11 return 0;
12 }

This program prints out 3.5. In this case we can also call sum by writing sum(1, 2.5).

Class templates are also possible, in much the same way we have written function templates:

1 #include <iostream>
2 using namespace std;
3
4 template <typename T>
5 class Point {
6 private:
7 T x, y;
8 public:
9 Point(const T u, const T v) : x(u), y(v) {}
10 T getX() { return x; }
11 T getY() { return y; }
12 };
13
14 int main() {
15 Point<float> fpoint(2.5, 3.5);
16 cout << fpoint.getX() << ", " << fpoint.getY() << endl;
17 return 0;
18 }

The program prints out 2.5, 3.5.

To declare member functions externally, we use the following syntax:

template <typename T>


T classname<T>::function_name()

So, for example, getX could have been declared in the following way:
template <typename T>
T Point<T>::getX() { return x; }

assuming a prototype of T getX(); inside the class definition.

We can also define different implementations for a single template by using


template specialization. Consider the following example:

1 #include <iostream>
2 #include <cctype>
3 using namespace std;
4
5 template <typename T>
6 class Container {
7 private:
8 T elt;
9 public:
10 Container(const T arg) : elt(arg) {}
11 T inc() { return elt+1; }
12 };
13
14 template <>
15 class Container <char> {
16 private:
17 char elt;
18 public:
19 Container(const char arg) : elt(arg) {}
20 char uppercase() { return toupper(elt); }
21 };
22
23 int main() {
24 Container<int> icont(5);
25 Container<char> ccont('r');
26 cout << icont.inc() << endl;
27 cout << ccont.uppercase() << endl;
28 return 0;
29 }

This program prints out 6 and R on separate lines. Here, the class Container is given
two implementations: a generic one and one specifically tailored to the char type. Notice
the syntax at lines 14 and 15 when declaring a specialization.

Finally, it is possible to parametrize templates on regular types:

1 #include <iostream>
2 using namespace std;
3
4 template <typename T, int N>
5 class ArrayContainer {
6 private:
7 T elts[N];
8 public:
9 T set(const int i, const T val) { elts[i] = val; }
10 T get(const int i) { return elts[i]; }
11 };
12
13 int main() {
14 ArrayContainer <int, 5> intac;
15 ArrayContainer <float, 10> floatac;
16 intac.set(2, 3);
17 floatac.set(3, 3.5);
18 cout << intac.get(2) << endl;
19 cout << floatac.get(3) << endl;
20 return 0;
21 }

This program prints out 3 and 3.5 on separate lines. Here, one instance of the
ArrayContainer class works on a 5-element array of ints whereas the other instance works
on a 10-element array of floats.

Default values can be set for template parameters. For example, the previous
template definition could have been:
template <typename T=int, int N=5> class ArrayContainer { ... }

and we could have created an ArrayContainer using the default parameters by writing:

ArrayContainer<> identifier;

2 Standard Template Library

Part of the C++ Standard Library, the Standard Template Library (STL) contains many useful
container classes and algorithms. As you might imagine, these various parts of the library are
written using templates and so are generic in type. The containers found in the STL are lists,
maps, queues, sets, stacks, and vectors. The algorithms include sequence operations, sorts,
searches, merges, heap operations, and min/max operations. We will explore how to use
some of these through example here:
1 #include <iostream>
2 #include <set>
3 #include <algorithm>
4 using namespace std;
5
6 int main() {
7 set<int> iset;
8 iset.insert(5);
9 iset.insert(9);
10 iset.insert(1);
11 iset.insert(8);
12 iset.insert(3);
13
14 cout << "iset contains:";
15 set<int>::iterator it;
16 for(it=iset.begin(); it != iset.end(); it++)
17 cout << " " << *it;
18 cout << endl;
19
20 int searchFor;
21 cin >> searchFor;
22 if(binary_search(iset.begin(), iset.end(), searchFor))
23 cout << "Found " << searchFor << endl;
24 else
25 cout << "Did not find " << searchFor << endl;
26
27 return 0;
28 }

In this example, we create an integer set and insert several integers into it. We then create an
iterator corresponding to the set at lines 14 and 15. An iterator is basically a pointer that provides
a view of the set. (Most of the other containers also provide iterators.) By using this iterator, we
display all the elements in the set and print out iset contains: 1 3 5 8 9. Note
that the set automatically sorts its own items. Finally, we ask the user for an integer,
search for that integer in the set, and display the result.
Here is another example:

1 #include <iostream>
2 #include <algorithm>
3 using namespace std;

4 void printArray(const int arr[], const int len) {


5 for(int i=0; i < len; i++)
6 cout << " " << arr[i];
7 cout << endl;
8 }
9
10 int main() {
11 int a[] = {5, 7, 2, 1, 4, 3, 6};
12
13 sort(a, a+7);
14 printArray(a, 7);
15 rotate(a,a+3,a+7);
16 printArray(a, 7);
17 reverse(a, a+7);
18 printArray(a, 7);
19
20 return 0;
21 }

This program prints out:

1 2 3 4 5 6 7
4 5 6 7 1 2 3
3 2 1 7 6 5 4

The STL has many, many more containers and algorithms that you can use. Read more at
http://www.cplusplus.com/reference/stl and http://www.cplusplus.com/reference/algorithm/.

3 Operator Overloading

We have been using operators on primitives, but sometimes it makes sense to use them
on user-defined datatypes. For instance, consider the following struct:
struct USCurrency {
int dollars;
int cents;
};

Perhaps we would like to add two USCurrency objects together and get a new one as a
result, just like in normal addition:
USCurrency a = {2, 50};
USCurrency b = {1, 75};
USCurrency c = a + b;

This of course gives a compiler error, but we can define behavior that our datatype should
have when used with the addition operator by overloading the addition operator. This can be
done either inside the class as part of its definition (the addition from the point of view of the
object on the left side of the +):
1 USCurrency operator+(const USCurrency o) {
2 USCurrency tmp = {0, 0};
3 tmp.cents = cents + o.cents;
4 tmp.dollars = dollars + o.dollars;
5
6 if(tmp.cents >= 100) {
7 tmp.dollars += 1;
8 tmp.cents -= 100;
9 }
10
11 return tmp;
12 }

or outside the class as a function independent of the class (the addition from the point of
view of the +):
1 USCurrency operator+(const USCurrency m, const USCurrency o) {
2 USCurrency tmp = {0, 0};
3 tmp.cents = m.cents + o.cents;
4 tmp.dollars = m.dollars + o.dollars;
5
6 if(tmp.cents >= 100) {
7 tmp.dollars += 1;
8 tmp.cents -= 100;
9 }
10
11 return tmp;
12 }

Similarly, we can overload the << operator to display the result:


1 ostream& operator<<(ostream &output, const USCurrency &o)
2 {
3 output << "$" << o.dollars << "." << o.cents;
4 return output;
5 }

Assuming the above definitions, we can run the following program:


1 int main() {
2 USCurrency a = {2, 50};
3 USCurrency b = {1, 75};
4 USCurrency c = a + b;
5 cout << c << endl;
6 return 0;
7 }

and get the printout $4.25.

The list of overloadable operators:

+ - * / += -= *= /= % %= ++ --
= == < > <= >= ! != && ||
<< >> <<= >>= & ^ | &= ^= |= ~
[] () , ->* -> new new[] delete delete[]

You might also like