Download CITS2210 Object-Oriented Programming Topic 16 C++: Templates

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Design Patterns wikipedia , lookup

Join-pattern wikipedia , lookup

Object-oriented programming wikipedia , lookup

Scala (programming language) wikipedia , lookup

Java syntax wikipedia , lookup

Class (computer programming) wikipedia , lookup

Java (programming language) wikipedia , lookup

Name mangling wikipedia , lookup

Covariance and contravariance (computer science) wikipedia , lookup

Java performance wikipedia , lookup

Go (programming language) wikipedia , lookup

C syntax wikipedia , lookup

C Sharp syntax wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

C++ wikipedia , lookup

Generic programming wikipedia , lookup

Transcript
CITS2210
Object-Oriented Programming
Topic 16
C++:
Templates in detail
Summary: This topic shows C++ templates in detail, and compares them
to Java generics. It also considers the much used C++ standard template
library, which includes templates for containers, similar to the Java
collections framework.
1
Templates
C++ templates are a powerful mechanism for generic programming.
–
They allow you to do very similar things to Java's generics.
–
They also allow you to do more.
–
However, they can more difficult to work with because templates are
not type checked until they used with a particular type.
–
This can make them harder to debug.
–
The following is a small example using templates.
#include <iostream>
using namespace std;
template <class T>
class mypair {
T a, b;
public:
mypair (T first, T second) { a=first; b=second; }
T getmax ();
};
template <class T>
T mypair<T>::getmax ()
{
T retval;
retval = a>b? a : b;
return retval;
}
int main () {
mypair <int> myobject (100, 75);
cout << myobject.getmax();
return 0;
}
2
Comparison with Java generics
You can generally program with C++ templates in a very similar way to
Java generics.
–
A Java generic class can be easily converted into a C++ class template,
and similarly for generic methods.
–
–
–
Both allow type parameters to be used as types in a parameterized
definition, which is later instantiated with particular types.
However, a key difference is that Java generics are compiled only once,
while C++ templates are type checked and compiled each time they are
used with a new set of type parameters.
This fundamental difference results in a number of important practical
differences between Java generics and C++ templates.
3
Comparison with Java generics - disadvantages
Type checking and compiling C++ templates separately for each
instantiation leads to the following disadvantages compared to Java
generics.
–
–
Type errors in a template definition are not found until the template is
instantiated with particular types.
–
This can make the type errors difficult to understand.
–
This is particularly true when multiple templates are involved.
–
There are even tools to help interpret the errors. (!)
C++ does not have any equivalent of an “extends” specification for type
parameters.
–
–
–
–
–
Instead, there will be a type error when compiling an instantiation
of the template if a type argument is inappropriate.
Generally there should be comments explaining any restrictions on
template parameters, similar to an extends specification.
Of course, these comments are not checked for consistency.
C++ templates can lead to large compiled programs.
C++ templates can't be separately compiled – all method definitions
must be in the same file as the interface. (Usually a header file.)
4
Comparison with Java generics - advantages
Type checking and compiling C++ templates separately for each
instantiation leads to the following advantages compared to Java generics.
–
–
–
The code can be optimized better because different compiled code is
produced for each set of instantiating types.
C++ allows special implementations of templates to be specified for
particular types. (See next page.)
C++ allows non-type parameters also in templates.
#include <iostream>
using namespace std;
// Example for non-type parameters
template <class T, int N>
class mysequence {
T memblock [N];
public:
void setmember (int x, T value);
T getmember (int x);
};
template <class T, int N>
void mysequence<T,N>::setmember (int x, T value) {
memblock[x]=value;
}
template <class T, int N>
T mysequence<T,N>::getmember (int x) {
return memblock[x];
}
int main () {
mysequence <int,5> myints;
mysequence <double,5> myfloats;
myints.setmember (0,100);
myfloats.setmember (3,3.1416);
cout << myints.getmember(0) << '\n';
cout << myfloats.getmember(3) << '\n';
return 0;
}
5
Example of template specialization
The following code defines a “container” template class (like Java's
collections), and a specialization for when the elements have type char.
// template specialization
#include <iostream>
using namespace std;
// class template:
template <class T>
class mycontainer {
T element;
public:
mycontainer (T arg) {element=arg;}
T increase () {return ++element;}
};
// class template specialization:
template <>
class mycontainer <char> {
char element;
public:
mycontainer (char arg) {element=arg;}
char uppercase ()
{
if ((element>='a')&&(element<='z'))
element+='A'-'a';
return element;
}
};
int main () {
mycontainer<int> myint (7);
mycontainer<char> mychar ('j');
cout << myint.increase() << endl;
cout << mychar.uppercase() << endl;
return 0;
}
6
C++ standard template library (STL)
The C++ standard template library includes many useful templates.
–
Containers are the most used templates, and are similar to Java's
collections framework.
–
This includes vector, linked list, hash table, etc.
–
Iterators are also central to the standard template library.
–
There are a number of other algorithms.
–
See: http://en.wikipedia.org/wiki/Standard_Template_Library for more
details.
7