对于inline的一点疑问
几乎所有的书上都讲inline函数在执行时是直接把函数体复制到调用处,所以提高了效率,但是我反汇编却发现无论是否用Inline修饰,都是直接call函数而并没有看到所谓的讲函数体复制到调用处,这是为什么?
以下是例子:
inline void fun(int n)
{
cout << n << endl;
}
int main(int argc, char* argv[])
{
fun(10);
return 0;
}
对应的反汇编代码:
15: int main(int argc, char* argv[])
16: {
00401780 push ebp
00401781 mov ebp,esp
00401783 sub esp,40h
00401786 push ebx
00401787 push esi
00401788 push edi
00401789 lea edi,[ebp-40h]
0040178C mov ecx,10h
00401791 mov eax,0CCCCCCCCh
00401796 rep stos dword ptr [edi]
17: fun(10);
00401798 push 0Ah
0040179A call @ILT+680(fun) (004012ad) //此处调用fun函数;
0040179F add esp,4
18:
19: return 0;
004017A2 xor eax,eax
20: }
在0040179A call @ILT+680(fun) (004012ad) 处按F11后直接到下面:
@ILT+680(?fun@@YAXH@Z):
004012AD jmp fun (004017d0)
接着F11,接到了函数里面,而并没有复制函数体内的代码:
10: inline void fun(int n)
11: {
004017D0 push ebp
004017D1 mov ebp,esp
004017D3 sub esp,40h
004017D6 push ebx
004017D7 push esi
004017D8 push edi
004017D9 lea edi,[ebp-40h]
004017DC mov ecx,10h
004017E1 mov eax,0CCCCCCCCh
004017E6 rep stos dword ptr [edi]
12: cout << n << endl;
004017E8 push offset @ILT+195(std::endl) (004010c8)
004017ED mov eax,dword ptr [ebp+8]
004017F0 push eax
004017F1 mov ecx,offset std::cout (0047c0c0)
004017F6 call @ILT+250(std::basic_ostream<char,std::char_traits<char> >::operator<<) (004010ff)
004017FB mov ecx,eax
004017FD call @ILT+475(std::basic_ostream<char,std::char_traits<char> >::operator<<) (004011e0)
13: }
请高手作答!