我试图用最简单的例子呈现给大家
#include "stdafx.h"
template <typename T> //定义一个模板
T Sum(T a, T b)
{
return a+b;
}
int main(int argc, char* argv[])
{
int m,n;
double x,y;
scanf("%d,%d",&m,&n);
scanf("%lf,%lf",&x,&y);
int a = Sum(m,n); //调用1
double b = Sum(x,y); //调用2
printf("%d\r\n",a);
printf("%f\r\n",b);
return 0;
}
最后附上 IDA反汇编代码
; int __cdecl main(int argc, const char **argv, const char **envp)
_main proc near
var_20= dword ptr -20h
var_1C= dword ptr -1Ch
var_18= qword ptr -18h
var_10= qword ptr -10h
var_8= qword ptr -8
argc= dword ptr 4
argv= dword ptr 8
envp= dword ptr 0Ch
sub esp, 20h
lea eax, [esp+20h+var_1C]
lea ecx, [esp+20h+var_20]
push eax
push ecx
push offset Format ; "%d,%d"
call _scanf
lea edx, [esp+2Ch+var_8]
lea eax, [esp+2Ch+var_10]
push edx
push eax
push offset aLfLf ; "%lf,%lf"
call _scanf
fld [esp+38h+var_10]
mov ecx, [esp+38h+var_20]
mov edx, [esp+38h+var_1C]
fadd [esp+38h+var_8]
add ecx, edx
push ecx
push offset aD ; "%d\r\n"
fstp [esp+40h+var_18]
call sub_401070
mov eax, dword ptr [esp+40h+var_18+4]
mov ecx, dword ptr [esp+40h+var_18]
push eax
push ecx
push offset asc_40A030 ; "%f\r\n"
call sub_401070
xor eax, eax
add esp, 4Ch
retn
_main endp
用F5反汇编后得到的结果
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // [sp+0h] [bp-20h]@1
int v5; // [sp+4h] [bp-1Ch]@1
double v6; // [sp+8h] [bp-18h]@1
double v7; // [sp+10h] [bp-10h]@1
double v8; // [sp+18h] [bp-8h]@1
scanf("%d,%d", &v4, &v5);
scanf("%lf,%lf", &v7, &v8);
v6 = v7 + v8;
sub_401070("%d\r\n", v5 + v4);
sub_401070("%f\r\n", *(_QWORD *)&v6);
return 0;
}