很多时候,要真正理解 c++ 一些特性的实现原理,最快的方式是自己亲自查看 c++ 代码对应的汇编代码。
本文记录一下 c++ 如何查看生成出来的汇编代码,以及如何看懂代码,部分内容参考自《深入理解计算机系统》[1]。
1. 先不看汇编
汇编的可读性挺差的,如果有得选,还是先用 cpp insights,看一看编译器角度生成的源码。cpp insights 的地址是 https://cppinsights.io/ ,官网对它的介绍[2]:
C++ Insights is a clang-based tool which does a source to source transformation. Its goal is to make things visible, which normally and intentionally happen behind the scenes. It’s about the magic the compiler does for us to make things work.
翻译过来就是:c++ insights 是一个基于 clang 的工具,用于执行源码到源码的转换。它的目标是让幕后的事情变得可见。关于编译器为了使事情正常工作所做的魔术。
直接看一下它能帮你洞察什么。
图1:cpp-insights-cpp-lambda
上面写了一小段 lambda 代码,c++ insights 帮忙生成出来了编译器视角的源码,从中我们可以清晰的看到 c++ 内部是如何实现 lambda 的。
用户的源码:
#include
int main() {
auto x = [](int a, int b) {
return a + b; };
int a = x(10, 20);
return 0;
}
cpp insights 翻译的源码:
#include
int main()
{
class __lambda_4_13
{
public:
inline /*constexpr */ int operator()(int a, int b) const
{
return a + b;
}
using retType_4_13 = int (*)(int, int);
inline constexpr operator retType_4_13 () const noex