Download presentation
Presentation is loading. Please wait.
1
1Genericity C++ Templates
2
2 Parametric Polymorphism Allows definitions to be parametrized at compile-time Such a definition is actually a “function” that returns a new program element(method, class, etc.) Template parameters = The arguments of this “function” AKA: type parameters Instantiation of a template == evaluating the “function”
3
3 Template Instantiation in C++ The compiler recompiles the template definition Substitutes the formal template parameters with the actual parameters Generates new object code Almost no compilation when the definition is read Most compilation work is at each instantiation point The template definition must be part of the source code That is: #included from an.h file Supporting separate compilation of templates is too difficult in practice
4
4 C++ Function Templates A function can have type parameters template T* create() { return new T(); } void f() { string* sp = create (); } we use create() thru explicit instantiation We specify the actual types inside a pair of In some cases the compiler can deduce the template parameters template void print(T& t) { cout << t << endl; } void g() { print(5); } We use print() thru implicit instantiation Possible if a type parameter is also a value parameter Make overload resolution more difficult Introduced later
5
5 C++ Class Templates template class Stack { Type buff[50]; int sp; public: Stack(void): sp(0) {} void push(const Type &e) { buff[sp++] = e; } Type pop(void) { return buff[--sp]; } int empty(void) const { return sp == 0;} int full(void) const{ return sp == 50; } }; template Stack ::push(T val){ if (sp >= size) { error("Stack is full"); return; } buff[sp++] = val; }
6
6 Kind of Parameters Generics are “routines executed at compile time and produce source code”. Therefore, all parameters must be entities that are known at compile time. Type: most frequent and most important. All languages which support genericity allow type parameter. Numerical Constant: e.g., the integer constant 3. Useful for building generic arrays. Variables: Constant: Address of a variable in C++ Routine: Constant: Address of function in C++
7
7 A Template Taking a Constant template struct FixedArray { double values[N]; int size() { return N; } // Could be static }; int main(int, char**) { FixedArray arr; arr.values[0] = 3.14; cout << "first element=" << arr.values[0] << endl; cout << "size=" << arr.size() << endl; return 0; }
8
8 A Template Taking a Function Pointer typedef void (*ErrorFunction)(const char *); template class Array { size_t n; Type *buff; public: Array(size_t n_): n(n_), buff(new Type[n]){ if ((buff == (Type *)0) error("Memory failure"); } Type & operator [] (size_t i) { if (i >= n){ error("Array overflow"); return buff[i]; } }... };
9
9 Using Function Arguments to Templates #include void err_abort(const char *msg) { cerr << "Error: " << msg <<". Aborting!\n"; exit(1); } typedef Array SafeIntArray;... SafeIntArray a(20);
10
10 String Arguments to Templates? template class NamedType: public Type { public: const char *name() { return Name; } }; #define NAMED(T) NamedType typedef NAMED(MyClass) MyNamedClass; Does not work! Consider const char *const Hello1 = "Hello"; const char *const Hello2 = "Hello"; Then, Hello1 and Hello2 are not (usually) the same!
11
11 A Template Taking a Type template struct Pair { void set(const T& x, const T& y) { a = x; b = y; } void print() { cout << a << "," << b << endl; } private: T a, b; }; typedef Pair StrPair; // Instantiate Pair, // pass char* as an argument typedef Pair IntPair; int main(int, char**) { StrPair sp; IntPair ip; sp.set("ab", "cd"); sp.print(); ip.set(10, 20); ip.print(); return 0; }
12
12Specialization template struct Pair { void set(const T& x, const T& y) { a = x; b = y; } void print() { cout << a << "," << b << endl; } private: T a, b; }; template<> struct Pair { void set(bool x, bool y) { v = (x?1:0) + (y?2:0); } void print() { cout << (v&1) << "," << (v&2) << endl; } private: int v; }; int main(int, char**) { Pair pb; pb.set(true, false); pb.print(); Pair pc; pc.set('x', 'y'); pc.print(); return 0; }
13
13 A Non Conforming Specialization is Legal template struct Pair { void set(const T& x, const T& y) { a = x; b = y; } void print() { cout << a << "," << b << endl; } private: T a, b; }; template<> struct Pair { void set(bool x, bool y) { v = (x?1:0) + (y?2:0); } public: int v; }; void doSomething() { Pair pb; pb.set(true, false); cout << pb.v << endl; Pair pc; pc.set('x', 'y'); pc.print(); pb.print(); // Error. Pair ::print() is undefined }
14
14 C++ Templates: Interim Summary A template is a “function” Arguments: types, constants Return value : A new class or a new function Recognized by the compiler The template is recompiled with each Instantiation Specialization == “if” Different result based on the actual arguments
15
15 Compile Time Computations template struct Factorial { enum { value = N * Factorial ::value }; }; template <> struct Factorial { enum { value = 1 }; }; void foo() { int x = Factorial ::value; // == 24 int y = Factorial ::value; // == 1 }
16
16 A Non-Terminating Compilation template struct Loop { typedef typename Loop >::Temp Temp; }; int main(int, char**) { Loop n; return 0; }
17
17 Constraints on Parameters? Constant parameters: type of argument. In C++, the usual type conversions are allowed, e.g., if formal parameter to class template is of type T*, then actual parameter can be of type T1*, provided that T1 is derived from T. Type of parameters: what is the type of a type? Main approaches: No restrictions Dynamic checking Explicit list of constraints By derivation
18
18 No Restriction on Arguments Any type is allowed. Generic limited to what can be done on all types. Example: Early versions of Eiffel. Advantages: Code sharing. Disadvantages: Restrictions on generics. Only the most primitive operations are allowed on arguments.
19
19 Dynamic Checking of Arguments (The generic equivalent of dynamic typing.) The compiler tries to instantiate a template. If an invalid operation is attempted, then an error message is issued. Example: C++ Advantages: Compiler writing is easy Flexibility to programmer Disadvantages: Extra burden on the library designer Surprises to the user Constraints are dependent on actual usage Difficult to understand Some parts of static type checking are deferred to link time
20
20 Explicit List of Constraints The programmer specifies the set of operations that might be used on a type argument. Example: Ada Advantages: Readability No surprises Disadvantages: Lists could be very long Less flexibility in the design of classes List of operations instead of abstraction of functionality
21
21 Constraints on Type Parameters (cont.) By derivation. The actual parameter must be the same or inherited from the declared type. Example: Current Eiffel Advantages: Code sharing OOP Like Abstraction at the right level Disadvantages: Could lead to over use of inheritance Inheritance is used for abstraction, not only for subtyping Too specific parameter types Too general parameter types Not appropriate for built-in types
22
22 Quiz: What are the 6 Constraints? template T avg(const T a[], int size) { T sum = a[0]; for (int i = 1; i < size; i++) sum += a[i]; return sum/size; }
23
23 Quiz: The 5 Constraints template class Stack { Type buff[50]; int sp; public: Stack(void): sp(0) {} void push(const Type &e) { buff[sp++] = e; } Type pop(void) { return buff[--sp]; } int empty(void) const { return sp == 0; } int full(void) const { return sp == 50; } };
24
24Instantiation Implicit: Instantiation occurs whenever an instance is required. Examples: ML, Function Templates in C++. Advantages: simple syntax. Disadvantages: Surprises. Requires a complex type inference engine: can be in some conditions an undecidable problem. Implicit Usage: Instantiation occurs whenever the programmer uses an instance of a generic for the first time. Examples: Class templates in C++. Optional in function templates in C++ (since November 1993). Advantages: Balance between convenience and accuracy. Disadvantages: Confusing error messages. Relatively complex syntax. Obscure environment of instantiation. Explicit: Programmer must instantiate a generic priorly to using it. Examples: Ada. C++ new features: explicit instantiation. Advantages: Explicit. No surprises. Disadvantages: What if the same generic is instantiated twice?
25
25 Example: Function Template Instantiation template T max(T a,T b) { return a > b ? a : b; } int main() { extern int f(int); int i(10), j = f(i); int k = max(i, j); extern double cos(double); double r(0.5), s(cos(r)); double t = max(r, s); return max(k, int(1/t)); }
26
26 Function Return Type? The function return type is not used for resolving ambiguities template inline long double sum(const T a[], int size){ return (long double) avg(a, size) * size; } template inline T sum(const T a[], int size) { return avg(a, size) * size; } The compiler will complain if the sum template is used
27
27 Class Template Instantiation Whenever a new particular version of the class template Stack is used (such as Stack or Stack ), the compiler will arrange for a version of that template to be instantiated. Each instantiation of template class is a class by itself. The template classes Stack, Stack, and Stack are different classes. There may be compile time errors when a template is instantiated. Only the members that are used should be instantiated. In a single compilation unit, instantiation of a template occurs only once for each particular set of values of the arguments.
28
28 Explicit Instantiation in C++ New feature: adopted in San Jose, November 1993. Ability to specify environment for instantiation. Pre-create libraries of instantiations. Independent of changes in the environment of the using programs. Some implementations: multiple explicit instantiation may produce an error message or warning. Explicit instantiation of a class template: forces instantiation of all its members, whereby forcing constraints. template class Stack ; Explicit instantiation of a function member of a class template: template void Stack ::push(int &);... Explicit instantiation of a function template:...
29
29 Explicit Instantiation of Function Template Given the function template: template T max(T a, T b) { return a > b ? a : b; } Explicit Instantiation is: template int max (int, int); // naming the argument template int max(int, int); // the argument is deduced here
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.