Skip to content

feat: improve the code sample for std::inplace_vector#73

Merged
Charliechen114514 merged 1 commit into
Awesome-Embedded-Learning-Studio:mainfrom
YukunJ:improve_inplace_vector
Jun 22, 2026
Merged

feat: improve the code sample for std::inplace_vector#73
Charliechen114514 merged 1 commit into
Awesome-Embedded-Learning-Studio:mainfrom
YukunJ:improve_inplace_vector

Conversation

@YukunJ

@YukunJ YukunJ commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Brief: improve the code sample for std::inplace_vector section.

Since the tutorial is explicitly mentioning

a.

最讨喜的一个性质是:T 是 trivially copyable 时,inplace_vector<T, N> 本身也是 trivially copyable
doc: https://cppreference.com/cpp/container/inplace_vector/try_push_back

b.

容量超限时的行为也设计得克制:push_back 超过 N 会抛 std::bad_alloc(异常关闭时退化为 terminate),而想避免异常可以用 C++26 的 try_push_back/try_emplace_back,它们超限时不抛、返回一个错误指示,适合 -fno-exceptions 环境。

It's worthwhile to show these two points in the code sample.

QA:
see godbolt link https://godbolt.org/z/K78EnTnYM

BTW:

I locally tested that, it seems like the memory footprint of std::inplace_vector is N * sizeof(T) + size_t, aka the raw underlying storage plus a size. But I wasn't sure if all standard implementation follows this convention. If you can find the cpp reference for this, feel free to add this point to the tutorial as well.

sizeof(std::inplace_vector<int, 4>>) = 20
sizeof(std::inplace_vector<int, 6>>) = 28
sizeof(std::inplace_vector<int, 11>>) = 48

@Charliechen114514

@YukunJ YukunJ changed the title feat: improve the code sample for std::inplace_vector feat: improve the code sample for std::inplace_vector Jun 21, 2026
@Charliechen114514

Copy link
Copy Markdown
Member

感谢改进!我确认了下代码没问题的!

sizeof 那条 BTW —— 查了 C++26 标准草案和再次确认cppreference, 确实没有依据,不建议加进教程。 我稍微确认了下,不管是cppreference还是C++标准草案,的确没有标准定义,找到最相近的是下面的这个

[inplace.vector.overview] §1 对布局的全部规定只有这句(见 https://eel.is/c++draft/inplace.vector.overview ):

An inplace_vector is a contiguous container. Its capacity is fixed and its elements are stored within the inplace_vector object itself.

cppreference也只是在inplace_vector最上面提到:

std::inplace_vector is a dynamically-resizable array with contiguous inplace storage. The elements of type T are stored and properly aligned within the object itself. The capacity of the internal storage is fixed at compile-time and is equal to N.

即标准只保证「连续容器 + 容量固定 + 元素存在对象自身内部」,似乎没有提到 sizeof / 对象总大小 / size 字段表示的任何规定。您测出来的 N * sizeof(T) + size_t(20/28/48)是 libstdc++/libc++ 的实现布局, —— 看起来应该属于实现定义,不是标准约定。所以的话,可能会将教程后续优化为「主流实现常见布局」的脚注式备注。

最后感谢您对本教程的持续贡献和关注!

@Charliechen114514 Charliechen114514 merged commit 473c183 into Awesome-Embedded-Learning-Studio:main Jun 22, 2026
4 checks passed
@YukunJ YukunJ deleted the improve_inplace_vector branch June 22, 2026 02:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants