Consider the following example:
struct vector {
int size() const;
bool empty() const;
};
bool vector::empty() const
{
return size() == 0;
}
The generated assembly code for vector::empty
(by clang, with optimizations):
push rax
call vector::size() const
test eax, eax
sete al
pop rcx
ret
Why does it allocate stack space? It is not used at all. The push
and pop
could be omitted. Optimized builds of MSVC and gcc also use stack space for this function (see on godbolt), so there must be a reason.