Skip to content

Commit

Permalink
Use const for span demos.
Browse files Browse the repository at this point in the history
We tell students to extensively use const, but the spans demo would
have allowed changing values in the original containers.

Fix hsf-training#352
  • Loading branch information
hageboeck committed Aug 21, 2024
1 parent c8179cd commit 4999519
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions talk/morelanguage/morestl.tex
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@

\begin{frame}[fragile]
\frametitlecpp[20]{\texttt{std::span<T>} - Usage}
\begin{exampleblockGB}{Some example}{https://godbolt.org/z/bTWjM8ba1}{\texttt{span}}
\begin{exampleblockGB}{Some example}{https://godbolt.org/z/nP3jr388z}{\texttt{span}}
\scriptsize
\begin{cppcode*}{}
void print(std::span<int> c) {
void print(std::span<const int> c) {
for (auto a : c) { std::cout << a << " "; }
std::cout << "\n";
}
Expand All @@ -70,16 +70,16 @@
print(a); // 23 45 67 89

std::vector v{1, 2, 3, 4, 5};
std::span<int> sv = v;
std::span<const int> sv = v;
print(sv.subspan(2, 2)); // 3 4

std::array a2{-14, 55, 24};
times2(a2);
print(a2); // -28 110 48

std::span<int, 3> sa2 = a2;
std::span<const int, 3> sa2 = a2;
std::cout << sizeof(sv) << " " << sizeof(sa2) << "\n"; // 16 8
std::span<int, 3> s2a2 = a; // compilation failure, invalid conversion
std::span<const int, 3> s2a2 = a; // compilation failure, invalid conversion
\end{cppcode*}
\end{exampleblockGB}
\end{frame}
Expand Down

0 comments on commit 4999519

Please sign in to comment.