Skip to content

Commit d74a1d4

Browse files
committed
Repair the snippet for 3.21 as in issue658
1 parent b79cbc0 commit d74a1d4

1 file changed

Lines changed: 27 additions & 8 deletions

File tree

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
/////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2018 - 2023.
2+
// Copyright Christopher Kormanyos 2018 - 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
66
//
77

8+
// Repaired in: https://github.com/ckormanyos/real-time-cpp/issues/658
9+
810
// chapter03_21-001_span.cpp
911

12+
#include <algorithm>
1013
#include <cstdint>
14+
#include <iomanip>
1115
#include <iostream>
1216
#include <iterator>
1317
#include <span>
18+
#include <sstream>
1419

15-
void clear_buffer(std::span<std::uint8_t> pb)
20+
namespace
1621
{
17-
for(auto u : pb)
22+
void clear_buffer(std::span<std::uint8_t> buf)
1823
{
19-
u = static_cast<std::uint8_t>(UINT8_C(0));
20-
21-
static_cast<void>(u);
24+
for(auto& next_byte : buf)
25+
{
26+
next_byte = static_cast<std::uint8_t>(UINT8_C(0));
27+
}
2228
}
2329
}
2430

@@ -33,6 +39,19 @@ void do_something()
3339

3440
int main()
3541
{
36-
// 0
37-
std::cout << unsigned(*(std::cend(legacy_buffer) - 1U)) << std::endl;
42+
std::span<std::uint8_t> buffer_span(legacy_buffer);
43+
44+
// First off, fill the legacy buffer with a pattern.
45+
std::fill(buffer_span.begin(), buffer_span.end(), UINT8_C(0x5A));
46+
47+
std::stringstream strm { };
48+
49+
// Ensure that the buffer has the pattern.
50+
strm << "filled: " << std::hex << std::uppercase << std::setw(std::streamsize { 2 }) << std::setfill('0') << unsigned(buffer_span.back());
51+
52+
// Now clear the buffer and the buffer has been cleared to 0.
53+
do_something(buffer_span);
54+
strm << "\ncleared: " << std::hex << std::uppercase << std::setw(std::streamsize { 2 }) << std::setfill('0') << unsigned(buffer_span.back());
55+
56+
std::cout << strm.str() << std::endl;
3857
}

0 commit comments

Comments
 (0)