string
objects,
the characters of a string were stored in memory,
and pointer pointed to the first character of the string.
p
points to the h in hello.
main.C
that should be written as a single template function.
min
whose full name is
std::min
because it belongs to the standard namespace
std.
min
functions is
::min,
because they do not belong to any namespace.
::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&,
we 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,
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.
double value,
only 5 digits to the right of the decimal point are printed.
fixed
and
setprecision.
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.
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 two
double
data members:
c++ main.C point.C
point
with float data members.
storm.cis.fordham.edu,
a float
occupies only half as many bytes as a
double.
c++ main.C point.C
point.
point.C file.
c++ main.C
counted
is not a template class.
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.
int,
long,
etc.
char_traits
and
iterator_traits.
vector
is a template class.
vectorint1.C
and
vectordouble1.C.)
list
(loop.C),
set,
and
pair
(pair.C).
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.
zipcode5
contains a five-digit United States zipcode.
zipcode9
contains a nine-digit United States zipcode.
zipcode
contains either a five-digit or nine-digit United States zipcode.
If you habitually use only
zipcode<5>,
not
zipcode<9>,
you’ll want to say
using zip = zipcode<5>; //from now on, zip means zipcode<5>
zip z {"10458"};
carnivore,
herbivore,
inert)
from the last version of the game.
#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++ -o ~/bin/game4 main.C wabbit.C terminal.C term.o -lcurses ls -l ~/bin/game4 game4
wolf,
rabbit,
boulder,
landmine
from the game.
#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.
cc -DUNIX= -c term.c ls -l term.o c++ -o ~/bin/game5 main.C wabbit.C terminal.C term.o -lcurses ls -l ~/bin/game5 game5
w
stands for “wide”;
uppercase
L
stands for “long”.
wolf
and
rabbit
do not suspect that these are now merely shorthands for
different flavors of
grandchild:
using rabbit = grandchild<randomly, herbivore, 'r'>; using wolf = grandchild<manual, carnivore, 'W'>;Similarly, most users of the the familiar class
string
and its “wide” sibling
wstring
do not suspect that these
have always been shorthands for different flavors of
basic_string:
using string = basic_string<char>; using wstring = basic_string<wchar_t>;
#include <string> string s {"hello"}; wstring ws {L"hello"};