CSE2305 - Object-Oriented Software Engineering
Week 10

Topic 20: C++ Templates


Remember¤ genericity¤?

Recall the linked list example

A better solution

Creating a C++ template

Using a C++ template

How to design a templated class¤

What parameters can a template have?

Function templates

Coping with exceptional behaviour¤

A scary example to finish with

template <long N>
class Factorial
	long Value(void) { return N * fn_1.Value(); }

	Factorial<N-1> fn_1;

template <>
class Factorial<0>
	long Value(void) { return 1; }

int main(void)
	Factorial<15> f15;

	cout << f15.Value() << endl;


This material is part of the CSE2305 - Object-Oriented Software Engineering course.
Copyright © Jon McCormack & Damian Conway, 1998-2005. All rights reserved.