{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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 In chapter 14 of the draft ANSI/ISO C++ standard is a mention of something called member templates. This feature is new in a way and not yet widely available, but worth mentioning here.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern