An Overview of Templates
␣␣␣␣}␣else␣{
␣␣␣␣␣␣␣␣return␣y;
␣␣␣␣}
}
inline␣float␣findMax(␣float␣x,␣float␣y␣)␣{
␣␣␣␣if␣(x<y)␣{
␣␣␣␣␣␣␣␣return␣x;
␣␣␣␣}␣else␣{
␣␣␣␣␣␣␣␣return␣y;
␣␣␣␣}
}
295
Armstrong, J 2016, C++ for Financial Mathematics, CRC Press, Boca Raton. Available from: ProQuest Ebook Central. [21 August 2017].
Created from uts on 2017-08-21 16:39:23.
296 C++ for Financial Mathematics
This code violates the Once and Only Once principle because we have written
essentially the same function twice, the only difference is that the types are
different in the different function declarations.
Templates provide a solution to this problem. The following single template
function allows us to find the maximum of either doubles or floats.
template ␣ < typename ␣T >
inline ␣ T ␣ findMax ( T ␣x , ␣ T ␣ y ) ␣ {
if ␣ ( x ␣ <␣ y ) ␣ {
return ␣ y ;
}
else ␣ {
return ␣ x ;
}
}
When you write a templated function, you don’t specify all the types
of the parameters to your function. Instead, you provide a list of dummy
variables, called template parameters, which can be replaced as necessary
with the required types. In this example, we have one template parameter
and it is called T.
In our example we specify that we are writing a template function and it
has a template parameter called T, by writing:
template ␣ < typename ␣T >
The remaining code in our template function is then a copy of the desired
code to compute the maximum but using the dummy variable T to represent
the type.
We can then use our findMax function equally well with any type that
provides an implementation of <. This includes float, double but also int
and even string. This is tested using the following code:
void ␣ testMax () ␣ {
Copyright © 2016. CRC Press. All rights reserved.
Armstrong, J 2016, C++ for Financial Mathematics, CRC Press, Boca Raton. Available from: ProQuest Ebook Central. [21 August 2017].
Created from uts on 2017-08-21 16:39:23.
An Overview of Templates 297
template is used because the types are not yet known. This means that users
of your library will need to have access to the code required to define all your
templated functions. This in turn means that these definitions need to be in
the header file.
Danger!
If you are writing a templated library function, the definitions and declarations
must be in the header files.
/* ␣ Destructor ␣ */
~ SimpleVector () ␣ {
delete [] ␣ data ;
}
/* ␣ Access ␣ data ␣ */
T ␣ get ( int ␣ index ) ␣ {
return ␣ data [ index ];
}
/* ␣ Access ␣ data ␣ */
void ␣ set ( int ␣ index , ␣ T ␣ value ) ␣ {
data [ index ] ␣ = ␣ value ;
}
Armstrong, J 2016, C++ for Financial Mathematics, CRC Press, Boca Raton. Available from: ProQuest Ebook Central. [21 August 2017].
Created from uts on 2017-08-21 16:39:23.
298 C++ for Financial Mathematics
private :
T * ␣ data ;
/* ␣ Rule ␣ of ␣ three ␣ -␣ we ␣ make ␣ these ␣ private ␣ */
SimpleVector ( const ␣ SimpleVector & ␣ o );
SimpleVector & ␣ operator =( const ␣ SimpleVector & ␣ o );
};
Armstrong, J 2016, C++ for Financial Mathematics, CRC Press, Boca Raton. Available from: ProQuest Ebook Central. [21 August 2017].
Created from uts on 2017-08-21 16:39:23.
An Overview of Templates 299
(ii) The option has a function payoff that takes the stock price at maturity
as a parameter and returns the payoff of that option.
(iii) The model has a function generateRiskNeutralPricePath that returns
a vector representing a simulated stock path in the risk-neutral measure.
This function must take as parameters: the time up to which we wish to
simulate the stock price path; the number of steps.
So long as all of these requirements are met, the following code will be able
to price the option using the given model.
Armstrong, J 2016, C++ for Financial Mathematics, CRC Press, Boca Raton. Available from: ProQuest Ebook Central. [21 August 2017].
Created from uts on 2017-08-21 16:39:23.
300 C++ for Financial Mathematics
model . drift ␣ = ␣ 0;
model . riskFreeRate ␣ = ␣ 0.1;
model . volatility ␣ = ␣ 0.2;
model . date ␣ = ␣ 0;
Armstrong, J 2016, C++ for Financial Mathematics, CRC Press, Boca Raton. Available from: ProQuest Ebook Central. [21 August 2017].
Created from uts on 2017-08-21 16:39:23.
An Overview of Templates 301
This solution may seem simpler than the solution we gave using polymor-
phism. However, it suffers from a number of significant problems.
Exercises
Copyright © 2016. CRC Press. All rights reserved.
17.3.1. Change the CallOption class in our template examples library so that
the word payoff is spelled incorrectly. Which file does the compiler say contains
the error?
Armstrong, J 2016, C++ for Financial Mathematics, CRC Press, Boca Raton. Available from: ProQuest Ebook Central. [21 August 2017].
Created from uts on 2017-08-21 16:39:23.
302 C++ for Financial Mathematics
17.4 Summary
Templates provide a solution to the Once and Only Once principle that
is very useful for writing data structure classes. On the other hand, using
templates to write unnecessarily generic code violates the YAGNI principle
(You Aren’t Going to Need It).
Templates can be used as an alternative to virtual functions, but virtual
functions are normally the better approach.
For financial mathematics problems it is a good idea to take advantage of
library classes that are built using templates, but to avoid writing your own
template libraries.
Copyright © 2016. CRC Press. All rights reserved.
Armstrong, J 2016, C++ for Financial Mathematics, CRC Press, Boca Raton. Available from: ProQuest Ebook Central. [21 August 2017].
Created from uts on 2017-08-21 16:39:23.