Вы находитесь на странице: 1из 2

Ru-Brd

2. Function Templates
O Template Iunctions deIine a Iamily oI Iunctions Ior diIIerent template arguments.
O en you pass template arguments, Iunction templates are instantiated Ior tese argument types.
O ou can explicitly qualiIy te template parameters.
O ou can overload Iunction templates.
O en you overload Iunction templates, limit your canges to speciIying template parameters explicitly.
O ake sure you see all overloaded versions oI Iunction templates beIore you call tem.

Ru-Brd


Ru-Brd


. Class Templates
O class template is a class tat is implemented wit one or more type parameters leIt open.
O To use a class template, you pass te open types as template arguments. Te class template is ten instantiated (and compiled) Ior
tese types.
O or class templates, only tose member Iunctions tat are called are instantiated.
O ou can specialize class templates Ior certain types.
O ou can partially specialize class templates Ior certain types.
O ou can deIine deIault values Ior class template parameters. Tese may reIer to previous template parameters.

Ru-Brd


Ru-Brd


. Non Type Templates
O Templates can ave template parameters tat are values rater tan types.
O ou cannot use Iloating-point numbers, class-type objects, and objects wit internal linkage (suc as string literals) as arguments
Ior non-type template parameters.
Ru-Brd


Ru-Brd


. Tricky Basics
O To access a type name tat depends on a template parameter, you ave to qualiIy te name wit a leading typename.
O ested classes and member Iunctions can also be templates. One application is te ability to implement generic operations wit
internal type conversions. However, type cecking still occurs.
O Template versions oI assignment operators don't replace deIault assignment operators.
O ou can also use class templates as template parameters, as so-called template template parameters.
O Template template arguments must matc exactly. DeIault template arguments oI template template arguments are ignored.
O By explicitly calling a deIault constructor, you can make sure tat variables and members oI templates are initialized by a deIault
value even iI tey are instantiated wit a built-in type.
O or string literals tere is an array-to-pointer conversion during argument deduction iI and only iI te parameter is not a reIerence.

Ru-Brd


Ru-Brd


. Using Templates.
O Templates callenge te classic compiler-plus-linker model. TereIore tere are diIIerent approaces to organize template code:
te inclusion model, explicit instantiation, and te separation model.
O &sually, you sould use te inclusion model (tat is, put all template code in eader Iiles).
O By separating template code into diIIerent eader Iiles Ior declarations and deIinitions, you can more easily switc between te
inclusion model and explicit instantiation.
O Te C standard deIines a separate compilation model Ior templates (using te keyword export). It is not yet widely available,
owever.
O Debugging code wit templates can be callenging.
O Template instances may ave very long names.
O To take advantage oI precompiled eaders, be sure to keep te same order Ior nclude directives.
Ru-Brd







Ru-Brd

. Basic Templates Terminology.

O Te term class template states tat te class is a template. Tat is, it is a parameterized description oI a Iamily oI classes.
O Te term template class on te oter and as been used
- as a synonym Ior class template.
- to reIer to classes generated Irom templates.
- to reIer to classes wit a name tat is a template-id.
Te process oI creating a regular class, Iunction, or member Iunction Irom a template by substituting actual values Ior its arguments is
called template instantiation. Tis resulting entity (class, Iunction, or member Iunction) is generically called a specialization.

speciIy explicitly a declaration tat is tied to a special substitution oI template parameters.

template< // explct specalaton
class MyClass<std::strng,float ,
.
<

tis is called a so-called explicit specialization

te general template is also called te primary template.

ODR basics:
O on-inline Iunctions and member Iunctions, as well as global variables and static data members sould be deIined only once
across te wole program.
O Class types (including structs and unions) and inline Iunctions sould be deIined at most once per translation unit, and all tese
deIinitions sould be identical.
translation unit is wat results Irom preprocessing a source Iile tat is, it includes te contents named by nclude directives.
It is essential to distinguis between template parameters and template arguments. In sort, you can say tat you "pass arguments to
become parameters." Or more precisely:
|2|
In te academic world, arguments are sometimes called actual parameters wereas parameters are called Iormal parameters.
O Template parameters are tose names tat are listed aIter te keyword template in te template declaration or deIinition (T and N
in our example).
O Template arguments are te items tat are substituted Ior template parameters (double and 10 in our example). &nlike template
parameters, template arguments can be more tan just "names."
Te substitution oI template parameters by template arguments is explicit wen indicated wit a template-id,
Ru-Brd

Вам также может понравиться