我正在分析按函數返回的值。見下面的實際問題:從gcc 5.3 -O0 -fno-elide-constructors
on godbolt返回值兩份
struct S{ int a, b, c, d; };
S f(){
S s;
s.b=5;
return s;
}
int main(){
S s= f();
s.a =2;
}
編譯器輸出:
f():
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
movl $5, -28(%rbp)
leaq -32(%rbp), %rdx
leaq -16(%rbp), %rax
movq %rdx, %rsi
movq %rax, %rdi
call S::S(S const&)
movq -16(%rbp), %rax
movq -8(%rbp), %rdx
leave
ret
main:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
call f()
movq %rax, -16(%rbp)
movq %rdx, -8(%rbp)
leaq -16(%rbp), %rdx
leaq -32(%rbp), %rax
movq %rdx, %rsi
movq %rax, %rdi
call S::S(S const&)
movl $2, -32(%rbp)
movl $0, %eax
leave
ret
什麼是我的問題嗎?
- 爲什麼有稱爲兩個拷貝構造函數?我不明白爲什麼它是必要的。
Nononono,代碼比鏈接更好。順便說一下,這個鏈接看起來像_crazy_並且被格式化爲代碼,所以沒有人可能會訪問它 – ForceBru
如果你不想在這裏發佈完整的代碼,那很好。但至少發佈了一個最小版本,其中包含您描述的問題 – BrainStone
並不真正有興趣點擊粗略鏈接。 – djechlin