Skip to content

Commit 74334dd

Browse files
fix: resolve all host example compilation errors (49/49 passing)
- Add missing includes (<cstdint>, <cstring>, <chrono>, <atomic>, <thread>, <utility>, <functional>, <iomanip>, <array>, <new>, <stdexcept>, <cstdarg>, <cmath>) - Fix type errors (sizes_t→size_t, int16_t for sine_table, uint8_t/uint16_t/uint32_t declarations) - Fix template issues (move templates out of function bodies, static→non-static member functions) - Fix lambda issues (duplicate captures, unused variables with -Werror, vexing parse, init capture syntax) - Fix std::expected API usage (std::unexpected for error returns) - Fix range/view issues (take_while, RingBuffer pipeline, pipe operator compatibility) - Upgrade C++ standards where needed (C++23 for <expected>, <chunk>) - Fix STM32 CI: use submodules: recursive for nested STM32CubeF1 - Improve build_examples.py: show error lines in failure output Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent eba9731 commit 74334dd

65 files changed

Lines changed: 919 additions & 796 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/build-examples.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
- name: Checkout repository
4141
uses: actions/checkout@v4
4242
with:
43-
submodules: true
43+
submodules: recursive
4444

4545
- name: Install build tools
4646
run: |

code/examples/chapter05/01_dynamic_memory_problems/fragmentation_demo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void fragmentation_scenario_1() {
5353
std::vector<void*> allocations;
5454

5555
// 分配不同大小的块
56-
sizes_t sizes[] = {16, 32, 64, 128, 256, 512, 1024};
56+
size_t sizes[] = {16, 32, 64, 128, 256, 512, 1024};
5757

5858
for (int round = 0; round < 3; ++round) {
5959
for (size_t size : sizes) {

code/examples/chapter05/02_static_vs_stack_allocation/static_allocation_demo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ int global_initialized = 100;
1111
int global_uninitialized;
1212

1313
// 3. 只读常量 (.rodata段 - 通常在Flash中)
14-
static const uint16_t sine_table[16] = {
14+
static const int16_t sine_table[16] = {
1515
0, 6424, 11773, 15836,
1616
18479, 19595, 19151, 17205,
1717
13938, 9605, 4479, 0,
@@ -59,7 +59,7 @@ void constexpr_static_demo() {
5959
}
6060

6161
// 演示静态存储用于查表
62-
uint16_t fast_sin(uint8_t angle) {
62+
int16_t fast_sin(uint8_t angle) {
6363
// 简化版:只演示查表访问
6464
return sine_table[angle % 16];
6565
}

code/examples/chapter05/03_object_pool_pattern/object_pool.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include <cstddef>
33
#include <cstdint>
4+
#include <cstring>
45
#include <new>
56
#include <type_traits>
67
#include <cassert>
@@ -117,7 +118,7 @@ class ObjectPool {
117118
size_t free_head_ = kInvalidIndex;
118119
size_t used_count_ = 0;
119120

120-
static size_t ptr_to_index(T* ptr) {
121+
size_t ptr_to_index(T* ptr) {
121122
uintptr_t base = reinterpret_cast<uintptr_t>(&storage_[0]);
122123
uintptr_t p = reinterpret_cast<uintptr_t>(ptr);
123124
EP_ASSERT(p >= base);

code/examples/chapter05/04_placement_new/basic_placement_new.cpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cstddef>
44
#include <cstdint>
55
#include <type_traits>
6+
#include <stdexcept>
67

78
// 演示placement new的基础用法
89

@@ -157,40 +158,40 @@ void alignment_check_demo() {
157158
if (aligned) {
158159
int* p = new (ptr) int(42);
159160
std::cout << " Successfully constructed int at " << p << "\n";
160-
p->~int();
161+
// No explicit destructor needed for trivial types
161162
}
162163
}
163164
}
164165

165166
// 在栈上构造"动态"大小的对象
166-
void stack_vector_demo() {
167-
std::cout << "\n=== Stack-Allocated \"Dynamic\" Container ===\n\n";
168-
169-
template<size_t N>
170-
class StackVector {
171-
alignas(double) unsigned char buffer_[N * sizeof(double)];
172-
size_t size_ = 0;
173-
174-
public:
175-
void push(double v) {
176-
if (size_ < N) {
177-
new (buffer_ + size_ * sizeof(double)) double(v);
178-
++size_;
179-
}
167+
template<size_t N>
168+
class StackVector {
169+
alignas(double) unsigned char buffer_[N * sizeof(double)];
170+
size_t size_ = 0;
171+
172+
public:
173+
void push(double v) {
174+
if (size_ < N) {
175+
new (buffer_ + size_ * sizeof(double)) double(v);
176+
++size_;
180177
}
178+
}
181179

182-
double& operator[](size_t i) {
183-
return *reinterpret_cast<double*>(buffer_ + i * sizeof(double));
184-
}
180+
double& operator[](size_t i) {
181+
return *reinterpret_cast<double*>(buffer_ + i * sizeof(double));
182+
}
185183

186-
size_t size() const { return size_; }
184+
size_t size() const { return size_; }
187185

188-
~StackVector() {
189-
for (size_t i = 0; i < size_; ++i) {
190-
reinterpret_cast<double*>(buffer_ + i * sizeof(double))->~double();
191-
}
186+
~StackVector() {
187+
for (size_t i = 0; i < size_; ++i) {
188+
reinterpret_cast<double*>(buffer_ + i * sizeof(double)); // trivial destructor, no-op
192189
}
193-
};
190+
}
191+
};
192+
193+
void stack_vector_demo() {
194+
std::cout << "\n=== Stack-Allocated \"Dynamic\" Container ===\n\n";
194195

195196
StackVector<10> vec;
196197
for (int i = 0; i < 5; ++i) {

code/examples/chapter05/04_placement_new/bump_allocator.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ class ScopedArena {
150150
// 析构时回退到初始位置
151151
// 注意:这不会调用析构函数!
152152
arena_.reset();
153-
arena_ = BumpAllocator(
154-
reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(&arena_) - initial_offset_),
155-
arena_.capacity()
156-
);
157153
}
158154

159155
// 禁止拷贝和移动

code/examples/chapter05/04_placement_new/inplace_wrapper.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <iostream>
2+
#include <cstdio>
3+
#include <string>
24
#include <new>
35
#include <type_traits>
46
#include <utility>
@@ -132,7 +134,7 @@ void basic_inplace_demo() {
132134
w->print();
133135

134136
w->update(10);
135-
w.print();
137+
w->print();
136138

137139
std::cout << "\nLeaving scope (auto destruct)...\n";
138140
}

code/examples/chapter05/06_array_vs_stdarray/basic_comparison.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ void data_access_demo() {
244244
std::cout << "\n";
245245
}
246246

247+
template<size_t N>
248+
void print_array_size(const std::array<int, N>&) {
249+
std::cout << "Array size (template param): " << N << "\n";
250+
}
251+
247252
void constexpr_demo() {
248253
std::cout << "\n=== Compile-Time Features ===\n\n";
249254

@@ -268,11 +273,6 @@ void constexpr_demo() {
268273
static_assert(fib.size() == 5, "Size must be 5");
269274

270275
// 作为模板参数
271-
template<size_t N>
272-
void print_array_size(const std::array<int, N>&) {
273-
std::cout << "Array size (template param): " << N << "\n";
274-
}
275-
276276
std::array<int, 10> arr;
277277
print_array_size(arr);
278278
}

code/examples/chapter05/06_array_vs_stdarray/practical_examples.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#include <array>
33
#include <algorithm>
44
#include <cstring>
5+
#include <cmath>
56
#include <cstdint>
7+
#include <cstdarg>
68

79
// std::array 在实际嵌入式场景中的应用
810

@@ -237,7 +239,7 @@ void lookup_table_demo() {
237239
std::cout << "\n=== Lookup Table with std::array ===\n\n";
238240

239241
// 正弦表(简化版)
240-
constexpr std::array<float, 16> sin_table = [] {
242+
std::array<float, 16> sin_table = [] {
241243
std::array<float, 16> table{};
242244
for (size_t i = 0; i < 16; ++i) {
243245
float angle = i * 3.14159f / 8.0f; // 0 to 2pi

code/examples/chapter06/02_unique_ptr_zero_overhead/polymorphism.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <memory>
55
#include <cstdio>
6+
#include <cstring>
67

78
// 基类必须有虚析构函数
89
struct Base {

0 commit comments

Comments
 (0)