C++ Template
C++ Templates - The Complete Guide
1
PDFword
C++ Template
zqding@hotmail.com
2
About This Book
C++ templates
templates
templates
C++ programming paradigms
C++ templates
template-based C++
templates
David C++ template
templates
Nico templates
Addison-Wesley
C++template tutorialC++template reference
C++ template
idioms
1.1
C++
classesinheritance
C++ iostreams , containers C++
templates
1998 C++ Standard[Standard98] C++
[Standard02]C++
[StroustrupC++PL][JosuttisOOP] [JosuttisLib]
C++ B.3.5
1.2
(1) templates (2)
templates tutorial
template constructs
C++ templates
templates
templates B C++overload
resolution
3 2
3
functors 22 smart pointers
20
1.3
C++ templates
templates
templates
templates
templates
templates
templates templates
http://www.josuttis.com/tmplbook/
1.4 Programming Style
C++
typesparametersvariables
void foo (const int &x);
void foo (const int& x);
void foo (int const &x);
void foo (int const& x);
constant integer int const
const intwhat is
constant const
const int N = 100;
int const N = 100;
//
//
int* const bookmark; // bookmark int
const *
syntactical substitution principle template
typedef char* CHARS;
typedef CHARS const CPTR; // chars const
CHARS
typedef char* const CPTR; // chars const
const
4
typedef char* CHARS;
typedef const CHARS CPTR; // chars const
CHARS
typedef const char* CPTR; // const chars
void foo (int const& x);
char *a, b;
C a b char
C++
C++
size_t ptrdiff_t std::
std::size_t size_t
1.5 vs. Standard versus Realit y
C++ Standard19982002
C++ Standard
C++ Standard
C++ Standard
C++Standard
C++ C++
C++ C++
templates 13
5
The Basic
C++templates function templates class templates
template nontype template
parameters typename member templates templates
Object-Oriented Programming in C++by Nicolai M. Josuttis, John Wiley & Sons Ltd., ISBN 0-
470-84399-3 templates C++
C++
Templates
C++ specific typesentities
quicksort
linked-list binary tree
1.
2. common base type Object void*
3. preprocessors, macros
CJava C++
1.
2. common base class
classes base classes
3. preprocessors C/C++ preprocessors
formatted source codescopetypes
Templates templates
functions classes templates
argument templates type checking
scope
Templates C++ templates
objects values
container classes stringTemplates
templates
6
2
Function Templates
function templates function templates
functions
2.1 Function Templates
Function templates function template
functions function
2.1.1 Template
function template
// basics/max.hpp
template
inline T const& max (T const& a, T const& b)
{
// a
typename T
typename type parameter
C++ template parameter template
parameter nontype parameter 4
Tidentifier T
TypeType parameters function template
T class T
T operator<
class typename type parameter
typename C++ class
type parameter class template max()
template
inline T const& max (T const& a, T const& b)
{
// a
// basics/max.cpp
#include
#include
#include "max.hpp"
int main()
{
int i = 42;
std::cout << "max(7,i): " << ::max(7,i) << std::endl;
double f1 = 3.4;
double f2 = -6.7;
std::cout << "max(f1,f2): " << ::max(f1,f2) << std::endl;
std::string s1 = "mathematics";
std::string s2 = "math";
std::cout << "max(s1,s2): " << ::max(s1,s2) << std::endl;
}
max() int doubl
std::string max()
max(7,i): 42
max(f1,f2): 3.4
max(s1,s2): mathematics
max() "::"
global namespace max() std::max() template
ambiguity
templates entity
max()
max()
int i = 42;
... max(7,i) ...
int template parameter T function template
inline int const& max (int const& a, int const& b)
{
// a c1, c2;
...
max(c1,c2);
// operator<
//
templates
1. template
2. template
8