Computer Engineering Rabie A. Ramadan [email protected] Lecture 5.

15
Computer Engineering Rabie A. Ramadan [email protected] Lecture 5

Transcript of Computer Engineering Rabie A. Ramadan [email protected] Lecture 5.

Page 1: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Computer Engineering

Rabie A. [email protected]

Lecture 5

Page 2: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Templates

2

Page 3: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Templates

3

A feature of the C++ programming language that allow functions and classes to operate with generic types

Allows a function or class to work on many different data types without being rewritten for each one.

A mold from which the compiler generates a family of classes or functions.

Page 4: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Function Templates

4

Behaves like a function that can accept arguments of many different types.

A function template represents a family of functions.

Page 5: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Function Templates declaration

5

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 * Constant2;

int const * Constant2;

int * const Constant3;

int const * const Constant4;

Page 6: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

const

6

const int * Constant2 ; Declares that Constant2 is variable pointer to a constant

integer

int const * Constant2; An alternative syntax which does the same,

int * const Constant3 Declares that Constant3 is constant pointer to a variable

integer

int const * const Constant4 Declares that Constant4 is constant pointer to a constant

integer.

Page 7: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Class Template

7

Page 8: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Members of Class Templates

8

Members of templated classes are defined differently than those of nontemplated classes.

Page 9: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Templates for Constructors and Destructors

9

Page 10: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Class Template Instantiation

10

B

Page 11: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Templates vs. Macros

11

#define SquareOf(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 = SquareOf(xin);

The formal syntax of a macro is:

#define name(dummy1[,dummy2][,...]) tokenstring

Page 12: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

How does a compiler handle a macro?

12

It gets handled and done with at compilation time rather than at run time.

When the compiler encounters a previously defined macro,• 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!

Page 13: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Why should that be a problem?

13

The following code compiles without any problem:

you probably expect the output of this program to be:

Page 14: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Why should that be a problem?

14

What you actually get, however, is this:

What happened?• When the compiler met the string "SquareOf(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. ?

Page 15: Computer Engineering Rabie A. Ramadan Rabie@rabieramadan.org Lecture 5.

Another vision

15

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.

On the other hand, when a macro is never invoked, its definition is completely irrelevant.

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:• http://www.ebyte.it/library/codesnippets/WritingCppMacros.html