110 likes | 230 Vues
Introduction to Genericity. Motivation Genericity vs. macros Stack example Max function example Templates as compiler procedures C++ Syntax. Motivation: Why Genericity?. Given is a piece of code, e.g., Definition Routine Class How can we reuse it? The usual benefits:
E N D
Introduction to Genericity • Motivation • Genericity vs. macros • Stack example • Max function example • Templates as compiler procedures • C++ Syntax oop
Motivation: Why Genericity? • Given is a piece of code, e.g., • Definition • Routine • Class • How can we reuse it? • The usual benefits: • Better overall quality • Lower maintenance cost • Save development efforts • New idea: • Make code applicable for many types. oop
Polymorphism • Polymorphism: (poly = many) + (morph = shape). • The same piece of code can be used for several things. move(Shape &s, Position p){ s.hide(); s += p; s.show();} • Code can be used with any sub-type (derived class) of Shape. • Dynamic binding: Polymorphism is realized in run-time---the same object code is applicable to many types. oop
Static Typing and Genericity • Dynamically-typed languages: • Any piece of code is polymorphic • It is the programmer responsibility to ensure that no run-time type error occurs • Statically-typed languages: two players game • Programmer: • Specify type of participants • Compiler: • Check that the code is used only with participants of the right type • Translate the code to target language using this assumption. • More efficient code In dynamically typed languages, these two tasks are done in run time • Genericity: Make the same piece of code usable for many different types, without compromising type safety and run time efficiency as in dynamic binding oop
Genericity vs. Dynamic Binding • Dynamic Binding: • 1 source code • 1 target code • Applicable to many types • Genericity: • 1 Meta source code • n source codes • n target codes • Each applicable to different type • Type correlation: Dynamic binding is applicable to a single object only. It is impossible to specify that two parameters are of the same run-time type. void swap(T& a, T& b){// run time type of a and // b might not be the same ...} oop
Inline Functions vs. Macros • Macros: • Difficult to write • Wrap arguments in parenthesis • Wrap macro in parenthesis • No clean C syntax • No type checking • Call by name: risky if argumentshave side effects • Awkward backslashes (\)if spans more than one line • General • Functions: • Type safe • Must repeat code for every type #define SUM(a,b) a+b // 2*SUM(3, 5) == 11 ! #define MULT(a,b) a*b // MULT(3+2, 5) == 13 ! #define STR char* STR a, b; // b is char #define PATH(x) x.paht() ... PATH(f); // error here! #define SQUARE(a) a*=a; SQUARE(x++); //x value??? oop
Macro to Define a Class • #define STACK_OF(T) StackOf##T • #define STACK_DECLARE(Type) \ • class STACK_OF(Type) { \ • Type buff[50]; \ • int sp; \ • public: \ • STACK_OF(Type)(void): sp(0) {} \ • void push(Type e) { buff[sp++] = e;} \ • Type pop(void) { return buff[--sp]; } \ • int empty(void){ return sp == 0; } \ • int full(void) { return sp == 50; } \ • } Awkward to write, edit, mail, reason about etc. Can your text editor align the ‘\’ so nicely? oop
Using The Macro Stack Class The main drawback: The C pre-processor never heard of C or C++. STACK_DECLARE(int); STACK_DECLARE(double); // STACK_DECLARE(char *) makes the compiler go bananas! typedefchar *Str; STACK_DECLARE(Str); StackOfint x1, x2; StackOfdouble y1, y2; StackOfStr z; Awkward to use! oop
True Genericity vs. Macros • Macros: parametrized text • Can be used to write a mail merge application... • Genericity: the ability to parametrize a lingual construct • Usually the parameter is a type. • Main examples of use of genericity • Container classes: in particular homogeneous containers. • Stack, queue, lists, binary trees, ... • Algorithms: • Sorting • Searching: breadth first, depth first, ... • Graph algorithms: connectivity, flow, ... oop
Example: Function Template in C++ “A clever kind of macro that obeys the scope, naming, and type rules of C++.” (Helpful oversimplification of B. Stroustrup) • Compactness and generality of macros • Type safe • Easy to write • // A template of function to compute the maximum // of two elements of any type: • template <class AnyType> • AnyType max(AnyType &a, AnyType &b) • { • return a > b ? a : b; • } oop
Some Useful Function Templates • Avoid redundant definition of operator!= when operator== is given: template <class T>inlinebooloperator != (const T& x, const T& y) {return !(x == y); } • Avoid redundant definitions of operators >, >= and <= when operator< is given: template <class T>inlinebooloperator >(const T& x, const T& y) {return y < x; } template <class T>inlinebooloperator <=(const T& x, const T& y) {return !(y < x); } template <class T>inlinebooloperator >=(const T& x, const T& y) {return !(x < y); } oop