Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 50a1f20

Browse files
bernhardmgrubersponce
authored andcommitted
Small fixes to concepts slides
1 parent 21f5a8a commit 50a1f20

File tree

1 file changed

+59
-32
lines changed

1 file changed

+59
-32
lines changed

‎talk/expert/cpp20concepts.tex‎

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
\begin{cppcode*}{}
2626
template
2727
<typename T,
28-
typename = typename std::enable_if_t<std::is_floating_point_v<T>>
28+
typename = std::enable_if_t<std::is_floating_point_v<T>>
2929
bool equal( T e1, T e2 ) {
30-
return abs(e1-e2)<std::numeric_limits<T>::epsilon();
30+
return std::abs(e1-e2)<std::numeric_limits<T>::epsilon();
3131
}
3232
... equal(10,5+5) ...
3333
\end{cppcode*}
@@ -53,7 +53,7 @@
5353
\frametitlecpp[20]{Basic requirements}
5454
\begin{block}{A new keyword}
5555
\begin{itemize}
56-
\item The keyword \mintinline{cpp}{requires} let us define various constraints.
56+
\item The keyword \mintinline{cpp}{requires} lets us define various constraints.
5757
\end{itemize}
5858
\end{block}
5959
\begin{exampleblock}{With concepts}
@@ -62,7 +62,7 @@
6262
template<typename T>
6363
requires std::is_floating_point_v<T>
6464
bool equal( T e1, T e2 ) {
65-
return abs(e1-e2)<std::numeric_limits<T>::epsilon();
65+
return std::abs(e1-e2)<std::numeric_limits<T>::epsilon();
6666
}
6767
... equal(10,5+5) ...
6868
\end{cppcode*}
@@ -95,7 +95,7 @@
9595
template< typename T>
9696
requires std::is_floating_point_v<T>
9797
bool equal( T e1, T e2 )
98-
{return abs(e1-e2)<std::numeric_limits<T>::epsilon();}
98+
{return std::abs(e1-e2)<std::numeric_limits<T>::epsilon();}
9999
\end{cppcode*}
100100
\end{exampleblock}
101101
\begin{block}{Requirements affect overload resolution}
@@ -110,54 +110,78 @@
110110
\begin{block}{Definition}
111111
\begin{itemize}
112112
\item a \textbf{concept} gives a name to a given set of requirements
113-
\item useful when requirements are reused often
113+
\item useful when the same requirements are reused frequently
114114
\end{itemize}
115115
\end{block}
116-
\begin{exampleblock}{A new keyword : {\itconcept}}
116+
\begin{exampleblock}{A new keyword: \texttt{concept}}
117117
\small
118118
\begin{cppcode*}{gobble=2}
119119
template< typename T>
120120
concept MyFloatingPoint =
121-
(std::is_floating_point_v<T>) &&
122-
(std::numeric_limits<T>::epsilon()>0);
121+
std::is_floating_point_v<T> &&
122+
std::numeric_limits<T>::epsilon()>0;
123123

124124
template<typename T>
125125
requires MyFloatingPoint<T>
126126
bool equal( T e1, T e2 )
127-
{return abs(e1-e2)<std::numeric_limits<T>::epsilon();}
127+
{return std::abs(e1-e2)<std::numeric_limits<T>::epsilon();}
128128
\end{cppcode*}
129129
\end{exampleblock}
130130
\end{frame}
131131

132132
\begin{frame}[fragile]
133133
\frametitlecpp[20]{Some usages of concepts}
134-
\begin{block}{Concepts as template parameters}
134+
\begin{block}{Constrained template parameters}
135135
\begin{itemize}
136-
\item concepts can be used in template parameter lists
137-
\item replacing \mintinline{cpp}{typename}
136+
\item a concept can replace \mintinline{cpp}{typename} in a template parameter lists
138137
\end{itemize}
139138
\end{block}
140139
\begin{exampleblock}{}
141140
\small
142141
\begin{cppcode*}{gobble=2}
143142
template<MyFloatingPoint T>
144143
bool equal( T e1, T e2 ) {
145-
return abs(e1-e2) < std::numeric_limits<T>::epsilon();
144+
return std::abs(e1-e2)<std::numeric_limits<T>::epsilon();
146145
}
147146
\end{cppcode*}
148147
\end{exampleblock}
149-
\begin{block}{Concepts in abbreviated function arguments}
148+
\begin{block}{Constrained variables}
150149
\begin{itemize}
151-
\item concepts can be used together with \mintinline{cpp}{auto} in abbreviated function templates
150+
\item a concept can be used together with \mintinline{cpp}{auto} to impose requirements on the type of a variable
151+
\end{itemize}
152+
\end{block}
153+
\begin{exampleblock}{firstnumber=4,gobble=2}
154+
\begin{cppcode*}{firstnumber=4,gobble=2}
155+
MyFloatingPoint auto f = 3.14f;
156+
MyFloatingPoint auto d = 3.14;
157+
MyFloatingPoint auto i = 3; // compile error
158+
\end{cppcode*}
159+
\end{exampleblock}
160+
\end{frame}
161+
162+
163+
\begin{frame}[fragile]
164+
\frametitlecpp[20]{Some usages of concepts}
165+
\begin{block}{Abbreviated function templates}
166+
\begin{itemize}
167+
\item function parameters, similar to variables, can be constrained as well
168+
\item the template head becomes obsolete
169+
\item the function is still a template though!
170+
\item unconstrained \mintinline{cpp}{auto} parameters are allowed as well
152171
\end{itemize}
153172
\end{block}
154173
\begin{exampleblock}{}
155174
\small
156-
\begin{cppcode*}{firstnumber=4,gobble=2}
175+
\begin{cppcode*}{gobble=2}
176+
// no template <...> here!
157177
bool equal( MyFloatingPoint auto e1,
158178
MyFloatingPoint auto e2 ) {
159-
return abs(e1-e2) <
160-
std::numeric_limits<decltype(e1)>::epsilon(); }
179+
return std::abs(e1-e2) <
180+
std::numeric_limits<decltype(e1)>::epsilon();
181+
}
182+
183+
// unconstrained abbreviated function template:
184+
void equal(auto e1, auto e2) { ... }
161185
\end{cppcode*}
162186
\end{exampleblock}
163187
\end{frame}
@@ -170,38 +194,39 @@
170194
\item Prefer the ones provided by the standard library
171195
\end{itemize}
172196
\end{block}
173-
\begin{exampleblock}{E.g.: the floating point concept}
197+
\begin{exampleblock}{E.g.: the floating point concept}
174198
\small
175199
\begin{cppcode*}{gobble=2}
176200
#include <concepts>
177201
bool equal( std::floating_point auto e1,
178202
std::floating_point auto e2 ) {
179-
return abs(e1-e2) <
203+
return std::abs(e1-e2) <
180204
std::numeric_limits<decltype(e1)>::epsilon();
181205
}
182206
\end{cppcode*}
183207
\end{exampleblock}
184208
\end{frame}
185209

186210
\begin{frame}[fragile]
187-
\frametitlecpp[20]{Concepts checking}
188-
\begin{block}{Concepts as boolean operators}
211+
\frametitlecpp[20]{Checking concepts}
212+
\begin{block}{Concepts as Boolean operators}
189213
\begin{itemize}
190-
\item Concepts can be used wherever a boolean is expected
214+
\item Concepts can be used wherever a \mintinline{c}{bool} is expected
191215
\item they can appear in \mintinline{cpp}{if constexpr} conditions
192216
\begin{itemize}
193217
\item since they are evaluated at compile-time
194218
\end{itemize}
195219
\end{itemize}
196220
\end{block}
197-
\begin{exampleblock}{Using concepts with {\itif constexpr}}
221+
\begin{exampleblock}{Using concepts with \texttt{if constexpr}}
198222
\small
199223
\begin{cppcode*}{gobble=2}
200224
template<typename T>
201225
bool equal( T e1, T e2 )
202226
{
203227
if constexpr (std::floating_point<T>) {
204-
return abs(e1-e2)<std::numeric_limits<T>::epsilon();
228+
return std::abs(e1-e2)
229+
< std::numeric_limits<T>::epsilon();
205230
} else {
206231
return (e1==e2);
207232
}
@@ -225,32 +250,34 @@
225250
\begin{cppcode*}{gobble=2}
226251
template<typename T>
227252
concept StreamableAndComparableNumber =
228-
requires( T v1, T v2 ) {
253+
requires( T v1, T v2, std::ostream os ) {
229254
requires std::integral<T> || std::floating_point<T>;
230-
std::cout<<v1<<v2;
255+
os<<v1<<v2;
231256
{ equal(v1,v2) } -> std::convertible_to<bool>;
232257
};
233258
\end{cppcode*}
234259
\end{exampleblock}
235-
Remember : use standard concepts first
260+
\begin{block}{}
261+
Remember : use standard concepts first
262+
\end{block}
236263
\end{frame}
237264

238265
\begin{frame}
239266
\frametitlecpp[20]{Requirements and concepts}
240-
\begin{block}{To be remembered}
267+
\begin{block}{Summary}
241268
\begin{itemize}
242269
\item A template can now \emph{require} properties of its parameters
243270
\item Compiler error messages clearly state which argument does not fulfill which requirement
244271
\item A set of requirements can be gathered in a \emph{concept}
245272
\item Overload resolution takes requirements into account
246273
\item The standard library provides many ready-to-use concepts
247-
\item Writing a new good concept is an expert topic
274+
\item Writing a new good concept is an expert task
248275
\end{itemize}
249276
\end{block}
250277
\end{frame}
251278

252279
\begin{frame}
253-
\frametitlecpp[11]{Exercise time}
280+
\frametitlecpp[20]{Exercise time}
254281
\begin{exercise}{Concepts}
255282
\begin{itemize}
256283
\item go to code/concepts

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /