22
Copyright, 2006 Oxford Consulting, Ltd - 1 - 29 March 2006 C++ Templates Templates Part of the ongoing development of the C++ language Integral part of the larger C++ Standard Library The Standard Template Library

- 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Embed Size (px)

Citation preview

Page 1: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 1 - 29 March 2006

C++ Templates

Templates

Part of the ongoing development of the C++ language

Integral part of the larger C++ Standard Library

The Standard Template Library

Page 2: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 2 - 29 March 2006

C++ Templates

Based upon the notion of a type variable

A variable that takes a type as its value

T i, j, k;Declares 3 variables

The type of the variables depends upon the value of T

If T is int variables are of type int float variables are of type float user-defined variables are of type user-defined

Page 3: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 3 - 29 March 2006

C++ Templates

Kinds of Templates

FunctionPermit the development of generic algorithms

Class

Permit the development of generic objects

Page 4: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 4 - 29 March 2006

C++ Templates

General Syntax

template < T0, T1… Tn> normal declaration or definition

template C++ keyword

Ti One or more formal parameters

A formal parameter

Must be written as: class Ti

Type parameter cannot have default value

Non-type parameter can have default values

Page 5: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 5 - 29 March 2006

C++ Templates

General Syntax - Parameters

Type Parameters

Built-in - Non-type int char float*

User Defined

Functions

In older compilers function templates cannot use non-type parameters.

The function definition can.

Classes

The class template can use non-type parameters.

Page 6: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 6 - 29 March 2006

C++ Templates

Function Templates

template <class T> T min (T var1, T var2);

The function min

Has two arguments: var1, var2 of type T

Returns a variable: of type T

template <class T> T min (T* ptVar1, T* ptVar2);

The function min

Has two arguments ptVar1, ptVar2 of type pointer to T

Returns a variable of type T

Page 7: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 7 - 29 March 2006

C++ Templates

Function Templates

A templated function can be declared extern inline static

template <class T> inline T min (T var1, T var2);

Page 8: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 8 - 29 March 2006

C++ Templates

Function Templates

Writing Function Templates

1. Write a specific version for each fixed type.

2. Compile and test each version.

3. Replace the fixed type with the type parameter.

4. Add the template header.

Page 9: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 9 - 29 March 2006

C++ Templates

Function Templates

Template Instantiation

1. The formal parameters in the function signature are examined in turn. A return type cast does not matter.

template <class T> T f(T);

int b;

double aValue = (double) f(b);

T has the value int.

2. Each formal parameter involving a type parameter is matched with the corresponding argument in the function call. The match binds the type parameter.

Page 10: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 10 - 29 March 2006

C++ Templates

Function Templates

Template Instantiation

3. All matches must be consistent. Only trivial promotions are

allowed.int& to const int&

4. Non-Type formal parameters must match without non-trivial conversion or promotion

Page 11: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 11 - 29 March 2006

C++ Templates

Functions

Function Call Resolution1. Examine all non-template functions

if an exact matchcall that version

if multiple matchescompile error

2. Examine all template functionsif an exact match

instantiate then call that versionif multiple matches

compile error

3. Re-examine all non-template functions

Apply call resolution rules for overloaded functions

Page 12: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 12 - 29 March 2006

C++ Templates

Class Templates

Particularly useful for making classes generic

Important application is container classes

Page 13: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 13 - 29 March 2006

C++ Templates

Class Templates

Class Template Template header

Normal Class Definition

template < class T0, … class Tn, NT0, NT1...>

class ClassName

{

class body

}

Page 14: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 14 - 29 March 2006

C++ Templates

Class Templates

Function Members Use the same template header as the class

Are instantiated when the compiler first sees a call to it

Syntax

template < class T0, … class Tn, NT0, NT1...>

returnType ClassName< class T0, … class Tn, NT0, NT1...>:: functionName (args)

{

function body

}

Page 15: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 15 - 29 March 2006

C++ Templates

Class Templates

Static Members

A templated class can have static members

Each type has an associated set of static members

Declarations

static T my Function (args) { myFunction body}

static int myValue // must be defined outside of the class

Definitiontemplate <class T> int MyClass<T>::myValue = aValue;

Page 16: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 16 - 29 March 2006

C++ Templates

Class TemplatesForward Declarations…..

template <class T> class MyClass // forward reference

….

….and Friends

template <class T> class MyClass

{

friend class YourClass<T>;

public:

……

}

Page 17: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 17 - 29 March 2006

C++ Templates

Class Templates

Declaring an Instance

template <class T> class MyClass

{

public:

……

}

MyClass <char> myInstance;

Observe the use of <type> as part of the class name

Page 18: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 18 - 29 March 2006

C++ Templates

Class Templates

Definition and Declaration If using header files, put the declaration and definition

both in the header file.

Templates as a type parameter The value of a type parameter can be a built-in or user

defined type.

The user defined type may be non-typed or typed.

Page 19: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 19 - 29 March 2006

C++ Templates

Class Templates

Templates and Inheritance

Derivation works the same as with ordinary classes

One can create a new template object from an existing

template

Page 20: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 20 - 29 March 2006

C++ Templates

Class Templates

Default Parameters Relatively new feature to templates A default value can be specified in a template definition Supported by most newer compilers.

Borland 5.0

Microsoft 4.0 and beyond

template <class T1, class T2 = int>

class MyClass

{ ….. }

Page 21: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 21 - 29 March 2006

C++ Templates

Function and Class Templates

SpecializationMay need to a special version of a template to handle types not easily included in an existing template.

FunctionreturnType functionName (args) { function body }

Member FunctionclassName < specialized type> functionMember

(args)

{ function body }

Classclass className < specialized type> { class body };

Page 22: - 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part

Copyright, 2006 Oxford Consulting, Ltd - 22 - 29 March 2006

C++ Templates

Function and Class Templates

Explicit Instantiation

May wish to create a specific version of a

function or class

FunctionreturnType functionName (args);

Classclass className < explicit type>