Computer Engineering Rabie A Ramadan Rabierabieramadan org Lecture
Computer Engineering Rabie A. Ramadan Rabie@rabieramadan. org Lecture 5
Templates 2
Templates l A feature of the C++ programming language that allow functions and classes to operate with generic types l Allows a function or class to work on many different data types without being rewritten for each one. l A mold from which the compiler generates a family of classes or functions. 3
Function Templates l l Behaves like a function that can accept arguments of many different types. A function template represents a family of functions. 4
Function Templates declaration l l template <typename T> Indicates that T is a template parameter Equivalent to template <class T> Variables declared with ‘const’ added become constants and cannot be altered by the program. Helps in error messages Which one is constant , the pointer or the variable ? const int * Constant 2; int const * Constant 2; int * const Constant 3; int const * const Constant 4; 5
const l l const int * Constant 2 ; Declares that Constant 2 is variable pointer to a constant integer int const * Constant 2; An alternative syntax which does the same, int * const Constant 3 Declares that Constant 3 is constant pointer to a variable integer int const * const Constant 4 Declares that Constant 4 is constant pointer to a constant integer. 6
Class Template 7
Members of Class Templates l Members of templated classes are defined differently than those of nontemplated classes. 8
Templates for Constructors and Destructors 9
Class Template Instantiation B 10
Templates vs. Macros l l l #define Square. Of(x) x*x It defines a kind of function which, used in an actual piece of code, Looks exactly like any other function call: Double yout, xin=3; yout = Square. Of(xin); The formal syntax of a macro is: #define name(dummy 1[, dummy 2][, . . . ]) tokenstring 11
How does a compiler handle a macro? l It gets handled and done with at compilation time rather than at run time. l When the compiler encounters a previously defined macro, • • l it first isolates its actual arguments, handling them as plain text strings separated by commas. It then parses the tokenstring, isolates all occurrences of each dummy-argument symbol and replaces it by the actual argument string. The whole process consists entirely of mechanical string substitutions with almost no semantic testing! 12
Why should that be a problem? l The following code compiles without any problem: you probably expect the output of this program to be: 13
Why should that be a problem? l What you actually get, however, is this: l What happened? • When the compiler met the string "Square. Of(xin+4)", it replaced it with the string "x*x" and then replaced each of the dummy-argument -strings "x" by the actual-argument-string "xin+4", obtaining the final string "xin+4*xin+4" which, in fact, evaluates to 19 and not to the expected 49. ? 14
Another vision l l • compilers ignore macros until they are invoked. If macro A(. . . ) contains a call to macro B(. . . ) there is no reason for the definitions of the two macros to appear in any particular order. If the definitions are in a header file, that of macro A may precede the one of macro B. It is only important that both macros be defined when macro A is actually called. l On the other hand, when a macro is never invoked, its definition is completely irrelevant. l Your header file may therefore contain nonsensical definitions of a number of unused macros and you will not find out until, at some later revision, you actually invoke one of them! Other problems could be found here: l • http: //www. ebyte. it/library/codesnippets/Writing. Cpp. Macros. html 15
- Slides: 15