实际上就是编译时生成了了两个函数:
先看cpp代码:
template<class T>
T get_min(T a, T b)
{
if(a > b)
return a;
return b;
}
int func(int &a, int *b)
{
a = get_min(a, *b);
float c = 0, d = 0;
c = get_min(c, d);
return 0;
}
这里定义一个模板函数get_min,然后再func里面分别进行int和float类型调用
我们先看引用和指针的区别:
Disassembly of section .text:
00000000 <_Z4funcRiPi>:
0: e92d4800 push {fp, lr}
4: e28db004 add fp, sp, #4
8: e24dd010 sub sp, sp, #16
c: e50b0010 str r0, [fp, #-16]//r0存放引用&a
10: e50b1014 str r1, [fp, #-20] ; 0xffffffec//r1存放指针
14: e51b3010 ldr r3, [fp, #-16]
18: e5932000 ldr r2, [r3]
1c: e51b3014 ldr r3, [fp, #-20] ; 0xffffffec
20: e5933000 ldr r3, [r3]
24: e1a01003 mov r1, r3
28: e1a00002 mov r0, r2
2c: ebfffffe bl 0 <_Z4funcRiPi>
30: e1a02000 mov r2, r0
34: e51b3010 ldr r3, [fp, #-16]
38: e5832000 str r2, [r3]
3c: e3a03000 mov r3, #0
40: e50b3008 str r3, [fp, #-8]
44: e3a03000 mov r3, #0
48: e50b300c str r3, [fp, #-12]
4c: e51b100c ldr r1, [fp, #-12]
50: e51b0008 ldr r0, [fp, #-8]
54: ebfffffe bl 0 <_Z4funcRiPi>
58: e50b0008 str r0, [fp, #-8]
5c: e3a03000 mov r3, #0
60: e1a00003 mov r0, r3
64: e24bd004 sub sp, fp, #4
68: e8bd4800 pop {fp, lr}
6c: e12fff1e bx lr
所以引用是用指针实现的
两外在嗲用