#include<iostream>
using namespace std;
/*
函数模板机制结论
编译器并不是把函数模板处理成能够处理任意类的函数
编译器从函数模板通过具体类型产生不同的函数
编译器会对函数模板进行两次编译
在声明的地方对模板代码本身进行编译;在调用的地方对参数替换后的代码进行编译。
根据调用产生不同类型的函数,本质上是c++编译器帮我们做了函数的重写,(本例生成两个函数)
*/
// 函数的业务逻辑 一样
// 函数的参数类型 不一样
void myswap01(int &a, int &b)
{
int c = 0;
c = a;
a = b;
b = c;
}
void myswap02(char &a, char &b)
{
char c = 0;
c = a;
a = b;
b = c;
}
template <typename T>
void myswap(T&a,T&b)
{
T c = 0;
c = a;
a = b;
b = c;
cout << "hello...我是函数模板,欢迎call我" << endl;
}
int main(void)
{
{
int x = 10;
int y = 20;
myswap<int>(x, y);//调用模板函数,显示调用
cout << "x=" << x << endl;
cout << "y=" << y << endl;
}
{
char a = 'a';
char b = 'b';
myswap<char>(a, b);
cout << "a=" << a << endl;
cout << "b=" << b << endl;
}
system("pause");
return 0;
}
/*
模板本质剖析.cpp
把此文档编译成汇编
预处理(Pre-Processing)
编译(Compiling)
汇编(Assembling)
链接(Linking)
Gcc *.c -o 1exe (总的编译步骤)
Gcc -E 1.c -o 1.i //宏定义进行宏展开
Gcc -S 1.i -o 1.s
Gcc -c 1.s -o 1.o
Gcc 1.o -o 1exe
*/
/*
本程序主要是为了编译成汇编语言来查看
模板的机制编译器会对函数模板进行两次编译.cpp
二次编译:第一次扫描函数模板的语法,有没有语法错误,可能生成函数头
当调用函数时生成具体的函数。
加一个断点 -调试-窗口-反汇编
*/
可以看到函数被调用了4次
.file "006\346\250\241\346\235\277\347\232\204\347\274\226\350\257\221\346\234\272\345\210\266_\345\257\271\346\250\241\346\235\277\350\277\233\350\241\214\344\270\244\346\254\241\347\274\226\350\257\221.cpp"
.text
.section .rodata
.type _ZStL19piecewise_construct, @object
.size _ZStL19piecewise_construct, 1
_ZStL19piecewise_construct:
.zero 1
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.text
.globl _Z8myswap01RiS_
.type _Z8myswap01RiS_, @function
_Z8myswap01RiS_:
.LFB1493:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movq %rdi, -24(%rbp)
movq %rsi, -32(%rbp)
movl $0, -4(%rbp)
movq -24(%rbp), %rax
movl (%rax), %eax
movl %eax, -4(%rbp)
movq -32(%rbp), %rax
movl (%rax), %edx
movq -24(%rbp), %rax
movl %edx, (%rax)
movq -32(%rbp), %rax
movl -4(%rbp), %edx
movl %edx, (%rax)
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1493:
.size _Z8myswap01RiS_, .-_Z8myswap01RiS_
.globl _Z8myswap02RcS_
.type _Z8myswap02RcS_, @function
_Z8myswap02RcS_:
.LFB1494:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movq %rdi, -24(%rbp)
movq %rsi, -32(%rbp)
movb $0, -1(%rbp)
movq -24(%rbp), %rax
movzbl (%rax), %eax
movb %al, -1(%rbp)
movq -32(%rbp), %rax
movzbl (%rax), %edx
movq -24(%rbp), %rax
movb %dl, (%rax)
movq -32(%rbp), %rax
movzbl -1(%rbp), %edx
movb %dl, (%rax)
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1494:
.size _Z8myswap02RcS_, .-_Z8myswap02RcS_
.section .rodata
.LC0:
.string "x="
.LC1:
.string "y="
.LC2:
.string "a="
.LC3:
.string "b="
.LC4:
.string "pause"
.text
.globl main
.type main, @function
main:
.LFB1496:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movq %fs:40, %rax
movq %rax, -8(%rbp)
xorl %eax, %eax
movl $10, -16(%rbp)
movl $20, -12(%rbp)
leaq -12(%rbp), %rdx
leaq -16(%rbp), %rax
movq %rdx, %rsi
movq %rax, %rdi
call _Z6myswapIiEvRT_S1_
leaq .LC0(%rip), %rsi
leaq _ZSt4cout(%rip), %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
movq %rax, %rdx
movl -16(%rbp), %eax
movl %eax, %esi
movq %rdx, %rdi
call _ZNSolsEi@PLT
movq %rax, %rdx
movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax
movq %rax, %rsi
movq %rdx, %rdi
call _ZNSolsEPFRSoS_E@PLT
leaq .LC1(%rip), %rsi
leaq _ZSt4cout(%rip), %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
movq %rax, %rdx
movl -12(%rbp), %eax
movl %eax, %esi
movq %rdx, %rdi
call _ZNSolsEi@PLT
movq %rax, %rdx
movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax
movq %rax, %rsi
movq %rdx, %rdi
call _ZNSolsEPFRSoS_E@PLT
movb $97, -16(%rbp)
movb $98, -12(%rbp)
leaq -12(%rbp), %rdx
leaq -16(%rbp), %rax
movq %rdx, %rsi
movq %rax, %rdi
call _Z6myswapIcEvRT_S1_
leaq .LC2(%rip), %rsi
leaq _ZSt4cout(%rip), %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
movq %rax, %rdx
movzbl -16(%rbp), %eax
movsbl %al, %eax
movl %eax, %esi
movq %rdx, %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c@PLT
movq %rax, %rdx
movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax
movq %rax, %rsi
movq %rdx, %rdi
call _ZNSolsEPFRSoS_E@PLT
leaq .LC3(%rip), %rsi
leaq _ZSt4cout(%rip), %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
movq %rax, %rdx
movzbl -12(%rbp), %eax
movsbl %al, %eax
movl %eax, %esi
movq %rdx, %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c@PLT
movq %rax, %rdx
movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax
movq %rax, %rsi
movq %rdx, %rdi
call _ZNSolsEPFRSoS_E@PLT
leaq .LC4(%rip), %rdi
call system@PLT
movl $0, %eax
movq -8(%rbp), %rcx
xorq %fs:40, %rcx
je .L5
call __stack_chk_fail@PLT
.L5:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1496:
.size main, .-main
.section .rodata
.align 8
.LC5:
.string "hello...\316\322\312\307\272\257\312\375\304\243\260\345\243\254\273\266\323\255call\316\322"
.section .text._Z6myswapIiEvRT_S1_,"axG",@progbits,_Z6myswapIiEvRT_S1_,comdat
.weak _Z6myswapIiEvRT_S1_
.type _Z6myswapIiEvRT_S1_, @function
_Z6myswapIiEvRT_S1_:
.LFB1733:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movq %rdi, -24(%rbp)
movq %rsi, -32(%rbp)
movl $0, -4(%rbp)
movq -24(%rbp), %rax
movl (%rax), %eax
movl %eax, -4(%rbp)
movq -32(%rbp), %rax
movl (%rax), %edx
movq -24(%rbp), %rax
movl %edx, (%rax)
movq -32(%rbp), %rax
movl -4(%rbp), %edx
movl %edx, (%rax)
leaq .LC5(%rip), %rsi
leaq _ZSt4cout(%rip), %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
movq %rax, %rdx
movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax
movq %rax, %rsi
movq %rdx, %rdi
call _ZNSolsEPFRSoS_E@PLT
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1733:
.size _Z6myswapIiEvRT_S1_, .-_Z6myswapIiEvRT_S1_
.section .text._Z6myswapIcEvRT_S1_,"axG",@progbits,_Z6myswapIcEvRT_S1_,comdat
.weak _Z6myswapIcEvRT_S1_
.type _Z6myswapIcEvRT_S1_, @function
_Z6myswapIcEvRT_S1_:
.LFB1737:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movq %rdi, -24(%rbp)
movq %rsi, -32(%rbp)
movb $0, -1(%rbp)
movq -24(%rbp), %rax
movzbl (%rax), %eax
movb %al, -1(%rbp)
movq -32(%rbp), %rax
movzbl (%rax), %edx
movq -24(%rbp), %rax
movb %dl, (%rax)
movq -32(%rbp), %rax
movzbl -1(%rbp), %edx
movb %dl, (%rax)
leaq .LC5(%rip), %rsi
leaq _ZSt4cout(%rip), %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
movq %rax, %rdx
movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax
movq %rax, %rsi
movq %rdx, %rdi
call _ZNSolsEPFRSoS_E@PLT
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1737:
.size _Z6myswapIcEvRT_S1_, .-_Z6myswapIcEvRT_S1_
.text
.type _Z41__static_initialization_and_destruction_0ii, @function
_Z41__static_initialization_and_destruction_0ii:
.LFB1988:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
cmpl $1, -4(%rbp)
jne .L10
cmpl $65535, -8(%rbp)
jne .L10
leaq _ZStL8__ioinit(%rip), %rdi
call _ZNSt8ios_base4InitC1Ev@PLT
leaq __dso_handle(%rip), %rdx
leaq _ZStL8__ioinit(%rip), %rsi
movq _ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rax
movq %rax, %rdi
call __cxa_atexit@PLT
.L10:
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1988:
.size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii
.type _GLOBAL__sub_I__Z8myswap01RiS_, @function
_GLOBAL__sub_I__Z8myswap01RiS_:
.LFB1989:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $65535, %esi
movl $1, %edi
call _Z41__static_initialization_and_destruction_0ii
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1989:
.size _GLOBAL__sub_I__Z8myswap01RiS_, .-_GLOBAL__sub_I__Z8myswap01RiS_
.section .init_array,"aw"
.align 8
.quad _GLOBAL__sub_I__Z8myswap01RiS_
.hidden __dso_handle
.ident "GCC: (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0"
.section .note.GNU-stack,"",@progbits