作者:手机用户2502917387 | 来源:互联网 | 2023-09-06 21:15
IplayedaroundwithGodboltsCompilerExplorer.Iwantedtoseehowgoodcertainoptimizationsare.
I played around with Godbolt's CompilerExplorer. I wanted to see how good certain optimizations are. My minimum working example is:
我试用了Godbolt的编译器浏览器。我想看看某些优化有多好。我的最小工作示例是:
#include
int foo() {
std::vector v {1, 2, 3, 4, 5};
return v[4];
}
The generated assembler (by clang 5.0.0, -O2 -std=c++14):
生成的汇编程序(clang 5.0.0, -O2 -std=c++14):
foo(): # @foo()
push rax
mov edi, 20
call operator new(unsigned long)
mov rdi, rax
call operator delete(void*)
mov eax, 5
pop rcx
ret
As one can see, clang knows the answer, but does quite a lot of stuff before returning. It seems to my that even the vector is created, because of "operator new/delete".
正如大家所看到的,clang知道答案,但是回来之前做了很多事情。在我看来,由于“操作符new/delete”,甚至连向量都被创建了。
Can anyone explain to me what happens here and why it does not just return?
谁能向我解释一下这里发生了什么,为什么它不会回来?
The code generated by GCC (not copied here) seems to construct the vector explicitly. Does anyone know GCC is not capable to deduce the result?
GCC生成的代码(这里没有复制)似乎明确地构造了这个向量。有没有人知道GCC不能推断出结果?
3 个解决方案