考虑以下例子:
struct vector {
int size() const;
bool empty() const;
};
bool vector::empty() const
{
return size() == 0;
}
通过优化的clang编译器生成的vector::empty
函数的汇编代码:
push rax
call vector::size() const
test eax, eax
sete al
pop rcx
ret
为什么它要分配栈空间?它根本没有被使用。我们可以省略 push
和 pop
。MSVC 和 GCC 的优化构建也会为此函数使用栈空间(请参见 godbolt),因此一定有原因。
this
参数吗? - dan04vector::size()
在示例中没有定义,以模拟它不是内联的。 - Dr. Guteax
中。不需要使用堆栈。 - Fred Larson