-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlecture14.tex
More file actions
614 lines (489 loc) · 15.3 KB
/
lecture14.tex
File metadata and controls
614 lines (489 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
\documentclass[unknownkeysallowed,xcolor=table]{beamer}
\usepackage [T2A,T1] {fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{amsmath}
\usepackage{pifont}
\usepackage{listings}
\usepackage{url}
\usepackage{textcomp}
\usepackage{multirow}
\usepackage{tabulary}
\newcommand{\cmark}{\ding{51}}
\newcommand{\xmark}{\ding{55}}
% to not copy line numbers
\usepackage{accsupp}
% to not copy line numbers
% to not copy line numbers
\newcommand{\noncopynumber}[1]{%
\BeginAccSupp{method=escape,ActualText={}}%
\color{blue} #1%
\EndAccSupp{}%
}
% to not copy line numbers
\setbeamertemplate{navigation symbols}{}
\newcommand{\textapprox}{\raisebox{0.5ex}{\texttildelow}}
\colorlet{mygreen}{green!60!blue}
\colorlet{mymauve}{red!60!blue}
\definecolor{pblue}{rgb}{0.1, 0.2, 0.8}
\newcommand{\rarr}{$\rightarrow$}
\lstset{
basicstyle=\ttfamily\small,
commentstyle=\color{mygreen},
keywordstyle=\color{blue},
% numberstyle=\tiny\color{blue},
numberstyle=\noncopynumber,
stringstyle=\color{mymauve},
numbers=left,
stepnumber=1,
columns=fullflexible,
breaklines=true,
postbreak=\mbox{\textcolor{red}{\ensuremath{\hookrightarrow}\space}},
literate={~} {\textapprox}{1},
language={[11]C++}
}
\makeatletter
\newcommand{\srcmediumsize}{\@setfontsize{\srcmediumsize}{7pt}{7pt}}
\makeatother
\makeatletter
\newcommand{\srcbigsize}{\@setfontsize{\srcbigsize}{8pt}{8pt}}
\makeatother
\makeatletter
\newcommand{\srcsize}{\@setfontsize{\srcsize}{6pt}{6pt}}
\makeatother
\makeatletter
\newcommand{\srcsmallsize}{\@setfontsize{\srcsmallsize}{5pt}{5pt}}
\makeatother
\title[C++]
{Программирование на языке C++}
\subtitle{Вводный курс}
\author[А.~Б.~Морозов]
{
\texorpdfstring{Александр Морозов\newline\href{mailto:gelu.speculum@gmail.com}{gelu.speculum@gmail.com}}
{Александр Морозов}
}
\date[ITMO 2020]
{ИТМО, весенний семестр 2020}
\logo{%
\makebox[0.97\paperwidth]{%
\includegraphics[align=c,width=2cm,keepaspectratio]{itmo_logo.png}
\hfill
\includegraphics[align=c,width=1.5cm,keepaspectratio]{itiviti_logo.png}
}
}
\AtBeginSection[]
{
\begin{frame}
\frametitle{Содержание}
\tableofcontents[currentsection]
\end{frame}
}
\begin{document}
\frame{\titlepage}
%-------------------------------------------------
\section{Шаблоны и параметры шаблонов}
\begin{frame}[fragile]{Шаблоны и параметры шаблонов}
\lstinline{template <} \emph{список шаблонных параметров} \lstinline{>} \emph{объявление}
\vspace{1em}
Параметры шаблонов:
\begin{itemize}
\item типы \lstinline{class Id} или \lstinline{typename Id} \vspace{0.5em}
\item шаблоны \lstinline{template <...> class Id} или \lstinline{template <...> typename Id} \vspace{0.5em}
\item значения \lstinline{type Id} или \lstinline{auto Id}
\end{itemize}
\vspace{1em}
Шаблонный идентификатор -- \emph{имя шаблона} \lstinline{< params list >}.
\end{frame}
\begin{frame}[fragile]{Пример шаблонов}
\begin{lstlisting}
template <class T>
class Queue { ... }; // template
Queue<int> q_int; // template-id
template <std::size_t Size>
class StackString { ... }; // template
void f(const StackString<10> & str); // template-id
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Параметры шаблонов со значением по умолчанию}
\begin{lstlisting}
template <class = int>
struct X {};
template <std::size_t Size = 10>
void f();
template <template <class> class T = X>
struct Y {};
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Переменное число шаблонных параметров}
\begin{lstlisting}[basicstyle=\ttfamily\srcbigsize]
template <class... Args, class T = char>
void g(Args &&... args);
template <class... Args>
void f(Args &&... args)
{
g<Args...>(std::forward<Args>(args)...);
}
f();
f(1, 0.5);
template <bool... Flags>
struct S {};
S<> zero;
S<false> one;
S<false, true> two;
template <template <class...> class... Templates>
class C {};
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Раскрытие переменного параметра шаблона}
\emph{нечто} \lstinline{...}
\vspace{2em}
\emph{нечто} должно включать в себя хотя бы один переменный параметр.
Идентификатор переменного параметра должен встречаться там, где допустимо использовать идентификатор.
\vspace{1em}
\emph{нечто} \lstinline{...} \rarr \emph{нечто1}, \emph{нечто2}, ...
\vspace{1em}
При раскрытии переменный параметр в \emph{нечто} заменяется на очередное значение параметра.
Если параметров было указано несколько, то все их наборы должны иметь одинаковую длину.
\end{frame}
\begin{frame}[fragile]{Примеры раскрытий переменных параметров}
\begin{lstlisting}[basicstyle=\ttfamily\srcbigsize]
template <class... Args>
void f(Args &&... args)
{
g(std::forward<Args>(args)...);
}
f(); // g();
f(1, 2, 3); // g(1, 2, 3);
template <class... Xs> struct Zip
{
template <class... Ys>
struct With
{
using type = std::tuple<std::pair<Xs, Ys>...>;
};
};
using X = Zip<int, double>::With<char, std::string>::type;
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Точки возможного раскрытия переменных параметров}
\begin{itemize}
\item список аргументов функции
\item список аргументов при вызове функции
\item список аргументов в скобочной инициализации
\item список параметров шаблонного идентификатора
\item список параметров шаблона
\item список наследования и список инициализации конструктора
\item специальная форма оператора \lstinline{sizeof}
\item операция свёртки
\item \lstinline{using}-объявление
\item список захвата лямбды
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Некоторые примеры раскрытия переменных параметров}
\begin{lstlisting}[basicstyle=\ttfamily\srcsmallsize]
template <int... Is>
void f()
{
std::tuple<decltype(Is)...> t(Is...); // direct initialization
std::initializer_list<int> i = { // list initialization
Is...
};
}
template<class... Ts, int... N>
void g(Ts (&...arr)[N]); // function parameters
g("Hello", "world");
// g(const char (&)[5], const char (&)[5])
template <class... T>
struct S
{
std::tuple<T...> data; // template-id
template <T... tt> // template parameters
struct Nested {};
};
template <class... Bases>
class Derived : Bases... // inheritance list
{
public:
Derived(const Bases & ...bases)
: Bases(bases)... // member initialization list
{}
using Bases::operator()...; // using declaration
};
template <class... T>
constexpr std::size_t count = sizeof...(T); // special sizeof
template <class... T>
constexpr std::size_t sum_sizes()
{
return (sizeof(T) + ...); // fold expression
}
\end{lstlisting}
\end{frame}
\begin{frame}{Шаблонные параметры-типы}
Имя параметра становится идентификатором типа внутри шаблона и ведёт себя, как псевдоним типа.
\vspace{2em}
При указании шаблонного параметра-типа это должен быть корректный идентификатор типа, в т.ч. неполного типа.
\end{frame}
\begin{frame}{Шаблонные параметры-значения}
Допустимые типы:
\begin{itemize}
\item lvalue ссылка \vspace{0.3em}
\item указатель \vspace{0.3em}
\item интегральный тип \vspace{0.3em}
\item \lstinline{enum} \vspace{0.3em}
\item \lstinline{std::nullptr_t}
\end{itemize}
\vspace{1em}
Значение при подстановке шаблонным параметром должно быть известно на этапе компиляции.
Параметр-указатель или параметр-ссылка не могут быть связаны со строковым литералом, подобъектом или временным объектом.
\vspace{1em}
Внутри шаблона такой параметр -- константное значение.
\end{frame}
\begin{frame}[fragile]{Шаблонные параметры-шаблоны}
Описание параметра имеет форму описания шаблона.
\vspace{1em}
Внутри шаблона такой параметр является шаблонным идентификатором.
\begin{lstlisting}[basicstyle=\ttfamily\srcbigsize]
template <template <class, bool> class X>
struct S {};
template <template <class K = void> class C>
void f()
{
C<> c;
C<int> cc;
}
template <class T>
struct SS {};
f<SS>();
\end{lstlisting}
\end{frame}
\begin{frame}{Виды шаблонов}
\begin{itemize}
\item переменные \vspace{0.5em}
\item функции \vspace{0.5em}
\item классы \vspace{0.5em}
\item псевдонимы типов
\end{itemize}
\vspace{1em}
Член класса может быть шаблонным (статическое поле, метод, вложенный класс или псевдоним типа).
\end{frame}
\begin{frame}[fragile]{Примеры шаблонов переменных}
\begin{lstlisting}[basicstyle=\ttfamily\srcbigsize]
template <class T>
T t_value = T();
struct S
{
template <int I>
static int x;
};
template <int I>
int S::x = I;
template <class T>
constexpr int t_tag = 11;
int main()
{
return t_value<int> + S::x<1> + t_tag<double>;
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Примеры шаблонов функций}
\begin{lstlisting}[basicstyle=\ttfamily\srcbigsize]
template <class T>
T max(const T & a, const T & b)
{
return a < b ? b : a;
}
template <class R, class T>
R convert(const T & t)
{
return t;
}
int main()
{
char a = '\n', b = '\t';
return convert<int>(max(a, b));
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Примеры шаблонов классов}
\begin{lstlisting}[basicstyle=\ttfamily\srcmediumsize]
template <class A, bool B>
struct S
{
A data;
static A s_data;
S();
bool get() const;
void set(const A & data_)
{ data = data_; }
};
template <class A, bool B>
A S<A, B>::s_data = A();
template <class A, bool B>
S<A, B>::S()
{}
template <class A, bool B>
bool S<A, B>::get() const
{ return B; }
int main()
{
S<int, false> s;
// s.set(11);
return s.data + s.s_data;
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Примеры шаблонов методов классов}
\begin{lstlisting}[basicstyle=\ttfamily\srcmediumsize]
struct S
{
template <class T>
operator T ();
};
template <class T>
S::operator T ()
{ return {}; }
template <class A, class B>
class C
{
template <class X>
void f();
};
template <class A, class B>
template <class X>
void C<A, B>::f()
{ }
int main()
{
C<int, char> c;
c.f<double>(); // ?
S s;
return s;
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Примеры шаблонов псевдонимов типов}
\begin{lstlisting}
template <class T>
using Map = std::map<T, T>;
template <class T>
using Identity = T;
\end{lstlisting}
\end{frame}
\section{Специализация шаблонов}
\begin{frame}[fragile]{Полная специализация шаблонов}
\begin{lstlisting}[basicstyle=\ttfamily\srcsize]
template <class T>
constexpr int t_tag = 11;
template <>
constexpr int t_tag<int> = 20;
template <>
constexpr int t_tag<char> = 30;
template <>
constexpr int t_tag<double> = 40;
template <class T>
T max(const T & a, const T & b)
{ return a < b ? b : a; }
template <>
int max(const int & a, const int & b)
{ return a - b; }
template <bool>
struct S;
template <>
struct S<false> {};
template <class T>
std::size_t check(const T & x)
{ return sizeof(S<t_tag<T> == 20>); }
int main()
{
check('a'); // ?
check(1); // ?
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Частичная специализация шаблонов}
\begin{lstlisting}[basicstyle=\ttfamily\srcsize]
template <class T>
constexpr int t_tag = 11;
template <template <class...> class L, class... Ts>
constexpr int t_tag<L<Ts...>> = 111;
template <class T, bool = true>
struct MakeNonAbstract
{
template <class... Args>
static std::unique_ptr<T> make(Args &&... args)
{ return {}; }
};
template <class T>
struct MakeNonAbstract<T, false>
{
template <class... Args>
static std::unique_ptr<T> make(Args &&... args)
{ return std::make_unique<T>(std::forward<Args>(args)...); }
};
template <class T, class... Args>
auto make_non_abstract(Args &&... args)
{
return MakeNonAbstract<T, std::is_abstract_v<T>>::make(std::forward<Args>(args)...);
}
struct A { virtual void f() = 0; };
int main()
{
auto p_a = make_non_abstract<A>();
auto p_i = make_non_abstract<int>(1);
return t_tag<std::tuple<>>;
}
\end{lstlisting}
\end{frame}
\section{Особенности использования шаблонов}
\begin{frame}[fragile]{Инстанциация шаблонов классов}
\begin{lstlisting}[basicstyle=\ttfamily\srcbigsize]
template <class T>
struct A;
template <class T>
struct B
{
T create()
{ return {}; }
void use(const T & x)
{ x.use(); }
};
int main()
{
A<int> * p_a = nullptr;
B<int> b;
return b.create();
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Разрешение неоднозначности зависимых имён}
\begin{lstlisting}
template <class T>
struct S
{
using value_type = typename T::value_type;
void f(const T & x)
{
x.template f<int>();
}
};
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{CRTP}
\begin{lstlisting}
template <class T>
class Base
{
public:
void f()
{
static_cast<T *>(this)->f_impl();
}
};
class Derived : public Base<Derived>
{
friend class Base<Derived>;
void f_impl();
};
\end{lstlisting}
\end{frame}
\end{document}