Templates

Templates - Templates INTRODUCTION TO C+ TEMPLATES PART 1 -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Templates INTRODUCTION TO C++ TEMPLATES PART 1 - FUNCTION TEMPLATES In issue #007 we talked about the use of inline functions. Suppose that you wish to compute the maximum of two quantities, and you define a C macro for this: #define max(a, b) ((a) > (b) ? (a) : (b)) This works OK until a case like: max(x++, y++) comes along. An inline function: inline int max(int a, int b) { return a > b ? a : b; } solves this problem. But what if you want a max() function for a variety of numeric types? You can define a slew of function prototypes: int max(int, int); long max(long, long); double max(double, double); and rely on function overloading to sort things out. Or you can define one function that might work in all cases: long double max(long double, long double); since nothing can be bigger than a long double, right? This last approach fails because there's no guarantee that, for example, the size of a long is less than the size of a long double, and assigning a long to a long double would in such a case result in loss of precision. In C++ there is another way to approach this problem, using what are called parameterized types or templates. We can define a function template: template <class T> inline T max(T a, T b) { return a > b ? a : b; } The preface "template <class T>" is used to declare a template. T is a template parameter, a type argument to the template. When this template is used: int a = 37; int b = 47; int i = max(a, b); the type value of T will be "int", because a and b are integers. If instead I had said: double a = 37.53; double b = -47.91; double d = max(a, b); then T would have the type value "double". The process of generating an actual function from a function template is known as "instantiation". Note also that "const
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
T&" may be used instead of "T"; we will be discussing this point in a future issue of the newsletter. This template will also work on non-numeric types, so long as they have the ">" operator defined. For example: class A { public: int operator>(const A&); // use "bool" return type // instead, if available }; A a; A b; A c = max(a, b); Templates are a powerful but complex feature, about which we will have more to say. Languages like C or Java(tm), that do not have templates, typically use macros or rely on using base class pointers and virtual functions to synthesize some of the properties of templates. Templates in C++ are a more ambitious attempt to support "generic programming" than some previous efforts found in other programming languages. Support for generic programming in C++ is considered by some to be as important a language goal for C++ as is support for object-oriented programming (using base/derived classes and virtual functions; see newsletter issue #008). An example of heavy template use can be found in STL, the Standard Template Library. NEW C++ FEATURE - MEMBER TEMPLATES
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

Templates - Templates INTRODUCTION TO C+ TEMPLATES PART 1 -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online