Академический Документы
Профессиональный Документы
Культура Документы
Webseiten: http://www.inf.ethz.ch/~zueff
23.10.2001, Lecture 1 1 of 30
C++ Templates
and Generic Programming
Overall Plan:
Part One: Template Mechanism in C++
Part Two: Technique Examples; Metaprogramming
Part Three: Introduction to Generic Programming
with Examples; STL Library
Detailed Plan:
Will be given for every lecture
23.10.2001, Lecture 1 5 of 30
C++ Templates
and Generic Programming
Literature:
ISO/IEC International Standard – C++
ISO/IEC 14882, 1998(E)
“Draft” Standard (publicly available) of Dec 1996.
www.cygnus.com/misc/wp/dec96pub
B. Stroustrup
The C++ Programming Language
Third Edition, AT&T 1997.
Leen Ammeraal
STL for C++ Programmers
John Whiley and Sons, Ltd., 1997.
ISBN: 0471971812.
23.10.2001, Lecture 1 6 of 30
C++ Templates
and Generic Programming
23.10.2001, Lecture 1 7 of 30
Introduction to Templates: The Very First Example
23.10.2001, Lecture 1 8 of 30
23.10.2001, Lecture 1 9 of 30
Introduction to Templates: The Very First Example
Straightforward Decision:
Define specific Max functions for all types
used in the program.
float Max ( float a, float b )
{
return a>b ? a : b;
}
double Max ( double a, double b )
{
return a>b ? a : b;
}
UT1 Max ( UT1 a, UT1 b )
{
UT1, UT2 – return a>b ? a : b;
user-defined }
types UT2 Max ( UT2 a, UT2 b )
{
return a>b ? a : b;
}
23.10.2001, Lecture 1 10 of 30
Straightforward Decision:
Define specific Max functions for all types
used in the program.
Why not?
• Very hard to maintain (i.e., to test & debug,
to prove correctness, to modify etc.).
• Impossible: type may be unknown beforehand
(e.g. in a case of a library).
23.10.2001, Lecture 1 11 of 30
Introduction to Templates: The Very First Example
T Max ( T a, T b )
{
return a>b ? a : b;
}
23.10.2001, Lecture 1 12 of 30
Keywords
Template Type
Parameter
23.10.2001, Lecture 1 13 of 30
Introduction: Template Declaration
23.10.2001, Lecture 1 14 of 30
res = Max(x-1,y+2.5);
23.10.2001, Lecture 1 15 of 30
Introduction: How It Works
Function template
Original call
template < typename T >
T Max ( T a, T b ) res = Max(x-1,y+2.5);
{
return a>b ? a : b;
}
Template Instantiation
Remarks:
• All the job is performed automatically by a C++ compiler
(this is the requirement of the Standard, i.e.,
this is the part of the language’s semantics).
• Compiler decides which actual types to substitute
to the function-by-template instead of formal type(s)
by analyzing the argument types from the call.
23.10.2001, Lecture 1 17 of 30
Introduction: How It Works
Remarks (continued):
• Template instantiation for a specific type is performed
only once.
Example: for the following two calls
res = Max(x-1,y+2.5);
res = Max(1.0,res);
23.10.2001, Lecture 1 18 of 30
Remarks (continued):
• Template instantiation is performed for every specific set
of the actual types.
Example: for the following two calls
res = Max(x-1,y+2.5);
int k = Max(1,(int
int)res);
int
23.10.2001, Lecture 1 19 of 30
Introduction: How It Works
23.10.2001, Lecture 1 20 of 30
f(actual-arguments)
Template Instantiation:
do A Problem for Future
23.10.2001, Lecture 1 22 of 30
// Generated by compiler
C MaxC ( C a, C b )
{
return a>b ? a : b;
}
23.10.2001, Lecture 1 24 of 30
The reason:
Class C must have the operator for making
the comparison of two objects of type C.
// Generated by compiler
C MaxC ( C a, C b )
{
return a>b ? a : b;
}
23.10.2001, Lecture 1 25 of 30
Template Instantiation: Requirements on Types
class C {
int m;
public:
public
C(): m(0) { }
bool operator > ( C& c )
{
return m > c.m;
}
};
Now it works.
23.10.2001, Lecture 1 26 of 30
Major conclusions:
1. It is possible to apply a template to objects of some type (either built-in
or user-defined type) if and only if this type contains all operators which
the template uses for objects of this type.
2. It is the good practice to specify requirements on actual type parameters
of a template, for example:
23.10.2001, Lecture 1 27 of 30
Templates or Macros?
// Template representation
template < typename T >
T Max ( T a, T b )
{
return a>b ? a : b;
}
// ”Traditional” representation
#define Max(a,b) ((a)>(b)?(a):(b))
23.10.2001, Lecture 1 28 of 30
23.10.2001, Lecture 1 29 of 30
Templates or Macros: Examples
char*
char p; No errors on compile time; Error message because
Max(p,1000); the result hardly makes sense. template declaration for Max
Also the result is of a statically requires the actual arguments
unknown type. The program is to be of the same type.
non-reliable and non-portable.
23.10.2001, Lecture 1 30 of 30
C++ Templates
and Generic Programming
Lecture 2
-
30.10.2001, Lecture 2 1 of 30
C++ Templates
and Generic Programming
Lecture 1: To Remind
• Function Template – A Family of Functions
• Template Type Parameters
• Template Instantiation
Lecture 2: Plan for Today
• Class Template – A Family of Types
• Class Template Instantiation
• Type and Non-Type Template Parameters
• Explicit Instantiation for Function Templates
30.10.2001, Lecture 2 2 of 30
30.10.2001, Lecture 2 3 of 30
Class Templates: The First Example
30.10.2001, Lecture 2 7 of 30
Class Templates: Implementation in C++
Class Template C
with Formal Type
Parameters T1 … Tn
Class by Template
C IJ1 … IJn
Actual Type
Parameters
IJ1 … IJn
30.10.2001, Lecture 2 9 of 30
Class Template Instantiation
Class Template
Stack with Formal
Type Parameter T
Class by Template
Stack<int>
Actual Type
Parameter
int
30.10.2001, Lecture 2 10 of 30
Template Name
Stack<int
int>
int Template Instantiation
Actual type
30.10.2001, Lecture 2 11 of 30
Class Template: Instantiation & Use
Declaration
Object Declaration
Stack<int
int>
int sint;
30.10.2001, Lecture 2 12 of 30
Stack<int
int>*
int ArrayOfStacks[10]; ? Oder Array von Textpointern ? D.S.
typedef Stack<double
double>
double SD;
SD sd1, sd2;
30.10.2001, Lecture 2 13 of 30
Class Templates: Major Conclusions
30.10.2001, Lecture 2 15 of 30
Class Templates: Requirements on Actual Types
So, this is the conclusion:
• If we want to apply our Stack template to a user-defined
type (i.e., to create a stack of class-type objects)
then we should provide copy constructor and assignment
operator in the class.
In other words:
• The requirement on the actual type from the Stack
template is that the actual type should always have
- public copy constructor and
- public assignment operator.
But:
Compiler automatically generates copy ctor & assignment
for every class. Does it mean that we don’t care about it?
30.10.2001, Lecture 2 16 of 30
30.10.2001, Lecture 2 17 of 30
Class Templates: Requirements on Actual Types
Hence there are direct reasons to weaken the requirement
on the actual type from the Stack template.
template < typename T >
class Stack
{
// implementation
. . .
public:
public
// interface
void push ( T& V ) { S[++top] = V; }
. . .
}
30.10.2001, Lecture 2 18 of 30
30.10.2001, Lecture 2 23 of 30
Template Instantiation: A Problem for Future
re
T any ( void )
tu
{
ec
return a random value of type T;
sl
}
iou
ev
But how to call this template?
pr
e
int i = any(); // is it correct?
th
mo
The question is:
Fr
how the compiler can determine the actual type
while instantiating the template?
30.10.2001, Lecture 2 24 of 30
30.10.2001, Lecture 2 26 of 30