docs(cpp): 补充 ch03 参数传递练习参考答案 - #201
Merged
Charliechen114514 merged 2 commits intoSep 3, 2026
Merged
Conversation
Charliechen114514
left a comment
Member
There was a problem hiding this comment.
反馈了一些建议级别的讨论,可以跟我聊聊您的看法~
|
|
||
| int main() | ||
| { | ||
| constexpr int kIterations = 100000; |
Member
There was a problem hiding this comment.
嗯嗯,看到了,但是constexpr出现在这里可能还是会对不熟悉的朋友造成一定的阅读困扰,可以的话换成宏先,然后在答案的后面说明一下,宏,经典的static const和constexpr之间的差异会更好
| user_admin | ||
| ``` | ||
|
|
||
| 原代码中的 `prefix` 是普通局部变量,只在 `get_prefix` 函数执行期间存在。函数返回时,它的生命周期结束,返回的 `const std::string&` 便指向已经销毁的对象,后续通过这个引用读取数据就是未定义行为。 |
Member
There was a problem hiding this comment.
嗯,这里可以补充一下,这是一种答案,但是实际上C++中,特别是在嵌入式里,很少会按值返回(当然现在C++17后NRVO强制了,其实这样写也OK)。编译器的行为约束从会倾向于移动(也就是把构造的资源不释放,从函数中直接把对象直接保留而不是销毁),但是返回值可能一些嵌入式的朋友会有疑问~,可以再补充下入参的引用形式~
Collaborator
Author
|
已换成宏定义和取消const对NRVO移动语义的干扰 |
Member
|
可以,approve了 |
Charliechen114514
merged commit Sep 3, 2026
1e45f61
into
Awesome-Embedded-Learning-Studio:main
5 checks passed
This was referenced Sep 5, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
变更类型
变更背景
完善《参数传递方式》一章的练习部分,为已有的三个练习补充可运行的参考答案,帮助学习者完成练习后进行自查,并进一步理解值传递、引用传递、const 引用和函数返回值生命周期等概念。
具体改动
修改文件:
documents/vol1-fundamentals/ch03/02-pass-by-value-ref.md新增内容:
练习一:实现 swap
double值的swap_values函数。std::string重载版本。main函数、编译命令和运行结果。练习二:高效处理大型结构体
double元素的Measurement。const引用传递版本的平均值计算函数。std::chrono对两种传递方式进行简单性能对比。练习三:修复悬垂引用
std::string。教学效果
本次补充覆盖了以下知识点:
const引用用于修改调用者对象。const T&在只读场景下的使用方式。所有参考答案均使用
::: details 参考答案折叠展示,不会影响学习者先独立完成练习。验证方式
git diff --cached --checkg++ 8.1.0编译并运行练习一示例:g++ -std=c++17 -Wall -Wextrag++ 8.1.0编译并运行练习二示例:g++ -std=c++17 -O2 -Wall -Wextrag++ 8.1.0编译并运行练习三示例:g++ -std=c++17 -Wall -Wextrapnpm build练习二的平均值计算结果为
500.5,Measurement大小为8000字节;练习三运行结果为user_admin。影响范围
本 PR 仅影响:
未修改导航、frontmatter 或其他章节内容。
关联 Issue / Discussion
暂无。