main.C
that should be written as a template function.
The C++ Standard library already has a function named
min
whose full name is
std::min
because it belongs to the standard namespace
std.
The full name of my
min
functions is
::min,
because they do not belong to any namespace.
I wrote
::min
to make sure I was calling my own
min
functions, not the one in the standard library.
To get the expression b < a
to compile when
b
and
a
are of data type
const date&,
I had to define the functions
operator<
or
operator int
for class
date.
if (b < a) { //if operator<(b, a) {
if (b < a) { //if (b.operator int() < a.operator int()) {
date.h:
a very simple class date,
just enough to make the main
function work.
date.Cmain.C,
main.txtc++ main.C date.C
main.C
that will work for every data type except
const char *.
The first three calls to min
in the main
function create three
instantiations
of the function template.
In the three instantiations,
the computer deduces
that the T
should be changed to
int,
double,
double,
const date&
respectively.
(An instantiation is also called an
implicit specialization.)
The T
is called a template argument.
As usual, the
a
and
b
are called
function arguments.
And the 10 and 20
are the actual function arguments.
date.h:
exactly the same class
date
as in the previous example.
date.Cmain.C,
main.txt:
exactly the same
main
function as in the previous program, but the rest of
main.C
is different.c++ main.C date.C
main.C.
date.h:
exactly the same class
date
as in the previous example.date.Cmain.C,
main.txt:
exactly the same main
function as in the previous example, but the rest of
main.C
is different.c++ main.C date.C
.h
(header) file.
min.h
contains the declarations and definitions of the
min template function.
See the important comment about the data type
T.
date.h:
exactly the same class
date
as in the previous example.date.Cmain.C,
main.txt:
exactly the same main
function as in the previous example, but the rest of
main.C
is different.c++ main.C date.C
10 2.71 5/1/2025 goodbyeSee this
min.h
for a better version
that passes function arguments of type
T
by reference, i.e., as a
const T&.
T.
min
in the C++ Standard Library does not know that the data type
const char *
probably needs to be handled differently.
print function has two arguments,
which may be of different data types.
c++ main.C date.C
print.h
contains the templatedate.h:
exactly the same class
date
as in the previous example.date.Cmain.C,
main.txtc++ main.C date.C
swap functions:
c++ main.C date.C
const T:
date.h:
exactly the same class
date
as in the previous example.date.Cswap.h
contains the templatemain.C,
main.txtc++ main.C date.C
#include <algorithm>
for the C++ Standard Library
swap.
min
template will not compile,
because our min
template requires two actual arguments of the same data type.
In this case,
the computer is unable to
deduce
which data type,
int or
double,
the T
should turn into.
c++ main.C
main.C: In function ‘int main()’:
main.C:14:22: error: no matching function for call to ‘min(int&, double&)’
14 | cout << ::min(i, d) << "\n"; //won’t compile
| ~~~~~^~~~~~
An explicit template argument
is also required when the template function takes no function arguments at all.
In this case, the return value of
f<double>
has one more correct digit than the return value of
f<float>.
That’s ten times as accurate!
c++ -std=c++20 pi.C
point
with double data members:
c++ main.C point.C
point
with float data members.
On our machine
storm.cis.fordham.edu,
a float
occupies only half as many bytes as a
double.
c++ main.C point.C
point.
There is no more point.C file.
c++ main.C
counted
is not a template class.
It has a static member function named
count
which receives no invisible argument.
c++ main.C counted.C
numeric_limits
is a template class with two static member
functions,
min
and
max.
c++ main.CWe will never create any object of classes
numeric_limits<int>,
numeric_limits<long>,
etc.
The only purpose of these classes is to give us information about the
data types
int,
long,
etc.
Other examples of classes that give us information about other data types
are
char_traits
and
iterator_traits.
vector
is a template class.
(See
vectorint.C
and
vectordouble.C.)
Ditto for classes
list
and
set.
c++ main.C
map
is a template class with two template arguments.
c++ map.C
typedef,
but contemporary C++ says
using.
c++ using.C
using.
The person who makes a
hydrogen
object will never suspect that
hydrogen
is a template class.
carnivore,
herbivore,
inert
from the game.
Instead of
#include’ing
the three header files
carnivore.h,
herbivore.h,
inert.h
in the header files for the four grandchild classes
wolf.h,
rabbit.h,
boulder.h,
landmine.h,
simply
#include
the following header file
position.h
in the header files for the four grandchild classes.
cc -DUNIX= -c term.c ls -l term.o c++ main.C wabbit.C terminal.C term.o -lcurses ls -l a.out ./a.out
wolf,
rabbit,
boulder,
landmine
from the game.
Instead of
#include’ing
the two header files
wolf.h
and
rabbit.h
in
main.C,
simply
#include
the following header file
grandchild.h
in
main.C.
Invent another new class of grandchild called a
looseCannon
that moves
randomly
(like a
rabbit)
and has the appetite of a
carnivore
(like a
wolf).
On the screen,
a loose cannon will appear as the character
c.
main.C
create grandchildren of all these different classes.
With the 3 motion and 3 rank classes we already have,
we could easily create 3 × 3 = 9
classes of grandchildren.
cc -DUNIX= -c term.c ls -l term.o c++ main.C wabbit.C terminal.C term.o -lcurses ls -l a.out ./a.out