原始笔记是一段口头描述加一整段代码,结构很平。这里按”用途 / 实现”两块整理,类的实现保持原样。
当前保留内容
1. 用途
hicc::debug::X 是一个专门用来调试 RVO、In-place construction、Copy Elision 等等特性的工具类,它平平无奇,只不过是在若干位置埋点冰打印 stdout 文字而已,这可以让我们直观观察到哪些行为实际上发生了。
X-class 在构造函数的入参部分有相似的构造(默认构造、移动构造、拷贝构造,以及对应的 operator=),用以区分每条路径被走到的时机。
2. 实现
namespace hicc::debug {
class X {
std::string _str;
void _ct(const char *leading) {
printf(" - %s: X[ptr=%p].str: %p, '%s'\n", leading, (void *) this, (void *) _str.c_str(), _str.c_str());
}
public:
X() {
_ct("ctor()");
}
~X() {
_ct("dtor");
}
X(std::string &&s)
: _str(std::move(s)) {
_ct("ctor(s)");
}
X(std::string const &s)
: _str(s) {
_ct("ctor(s(const&))");
}
X &operator=(std::string &&s) {
_str = std::move(s);
_ct("operator=(&&s)");
return (*this);
}
X &operator=(std::string const &s) {
_str = s;
_ct("operator=(const&s)");
return (*this);
}
const char *c_str() const { return _str.c_str(); }
operator const char *() const { return _str.c_str(); }
};
} // namespace hicc::debug
后续可补的方向
- 配套一组最小可复现 demo:分别调用按值返回、
emplace、std::move等,对照 stdout 看走到了哪个_ct分支 - 加上 NRVO / pre-C++17 强制 RVO / C++17 复制省略的版本差异说明
FEATURED TAGS
Git
Cheat Sheet
Markdown
Tools
C++
Linker
Thread
Linux
TCP
Network
GDB
Debug
leetcode
链表
WSL
Ubuntu
Windows
Linux Kernel
GCC
Android
adb
Troubleshooting
Profiling
Sanitizer
glibc
MySQL
Database
Python
curl
Build
ELF
clang-format
CMake
Graphviz
Performance
vcpkg
Protobuf
排查
速查
内存
STL
调试
性能分析
性能
读书笔记
方法论
架构
网络
Timer
mbedTLS
TLS
安全
负载均衡
脚本
工具
LRU
二叉树
BST
中序遍历
回溯
二分查找
优先队列
排序
旋转数组
jenkins
部署