深入分析泛型编程--编译器使用模版生成代码

 
#include<iostream>
using namespace std;
template<typename T>
T * My_find(T *array,T n,T x){
    T* p = array;
    int i;
    for(i=0;i<n;i++){
        if(*p == x){
            return p;
        }
        p++;
    }
    return 0;
}
int main(){
    int a[] = {1,2,3,4,5,6,7,8,10};
    int *result = My_find<int>(a,10,3);
    cout<<(*result)<<"\n"<<endl;

    char x[] = {'a', 'b', 'c', 'd'};

    char *res = My_find<char>(x,4,3);
    cout<<(*result)<<"\n"<<endl;
    return 0;
}
.LC0:
        .string "\n"
main:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 80
        mov     DWORD PTR [rbp-64], 1
        mov     DWORD PTR [rbp-60], 2
        mov     DWORD PTR [rbp-56], 3
        mov     DWORD PTR [rbp-52], 4
        mov     DWORD PTR [rbp-48], 5
        mov     DWORD PTR [rbp-44], 6
        mov     DWORD PTR [rbp-40], 7
        mov     DWORD PTR [rbp-36], 8
        mov     DWORD PTR [rbp-32], 10
        lea     rax, [rbp-64]
        mov     edx, 3
        mov     esi, 10
        mov     rdi, rax
        call    int* My_find<int>(int*, int, int)
        mov     QWORD PTR [rbp-8], rax
        mov     rax, QWORD PTR [rbp-8]
        mov     eax, DWORD PTR [rax]
        mov     esi, eax
        mov     edi, OFFSET FLAT:_ZSt4cout
        call    std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
        mov     esi, OFFSET FLAT:.LC0
        mov     rdi, rax
        call    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
        mov     esi, OFFSET FLAT:_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
        mov     rdi, rax
        call    std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))
        mov     DWORD PTR [rbp-68], 1684234849
        lea     rax, [rbp-68]
        mov     edx, 3
        mov     esi, 4
        mov     rdi, rax
        call    char* My_find<char>(char*, char, char)
        mov     QWORD PTR [rbp-16], rax
        mov     rax, QWORD PTR [rbp-8]
        mov     eax, DWORD PTR [rax]
        mov     esi, eax
        mov     edi, OFFSET FLAT:_ZSt4cout
        call    std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
        mov     esi, OFFSET FLAT:.LC0
        mov     rdi, rax
        call    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
        mov     esi, OFFSET FLAT:_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
        mov     rdi, rax
        call    std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))
        mov     eax, 0
        leave
        ret
int* My_find<int>(int*, int, int):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-24], rdi
        mov     DWORD PTR [rbp-28], esi
        mov     DWORD PTR [rbp-32], edx
        mov     rax, QWORD PTR [rbp-24]
        mov     QWORD PTR [rbp-8], rax
        mov     DWORD PTR [rbp-12], 0
.L7:
        mov     eax, DWORD PTR [rbp-12]
        cmp     eax, DWORD PTR [rbp-28]
        jge     .L4
        mov     rax, QWORD PTR [rbp-8]
        mov     eax, DWORD PTR [rax]
        cmp     DWORD PTR [rbp-32], eax
        jne     .L5
        mov     rax, QWORD PTR [rbp-8]
        jmp     .L6
.L5:
        add     QWORD PTR [rbp-8], 4
        add     DWORD PTR [rbp-12], 1
        jmp     .L7
.L4:
        mov     eax, 0
.L6:
        pop     rbp
        ret
char* My_find<char>(char*, char, char):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-24], rdi
        mov     ecx, esi
        mov     eax, edx
        mov     edx, ecx
        mov     BYTE PTR [rbp-28], dl
        mov     BYTE PTR [rbp-32], al
        mov     rax, QWORD PTR [rbp-24]
        mov     QWORD PTR [rbp-8], rax
        mov     DWORD PTR [rbp-12], 0
.L12:
        movsx   eax, BYTE PTR [rbp-28]
        cmp     DWORD PTR [rbp-12], eax
        jge     .L9
        mov     rax, QWORD PTR [rbp-8]
        movzx   eax, BYTE PTR [rax]
        cmp     BYTE PTR [rbp-32], al
        jne     .L10
        mov     rax, QWORD PTR [rbp-8]
        jmp     .L11
.L10:
        add     QWORD PTR [rbp-8], 1
        add     DWORD PTR [rbp-12], 1
        jmp     .L12
.L9:
        mov     eax, 0
.L11:
        pop     rbp
        ret
__static_initialization_and_destruction_0(int, int):
        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     DWORD PTR [rbp-4], edi
        mov     DWORD PTR [rbp-8], esi
        cmp     DWORD PTR [rbp-4], 1
        jne     .L15
        cmp     DWORD PTR [rbp-8], 65535
        jne     .L15
        mov     edi, OFFSET FLAT:_ZStL8__ioinit
        call    std::ios_base::Init::Init() [complete object constructor]
        mov     edx, OFFSET FLAT:__dso_handle
        mov     esi, OFFSET FLAT:_ZStL8__ioinit
        mov     edi, OFFSET FLAT:_ZNSt8ios_base4InitD1Ev
        call    __cxa_atexit
.L15:
        nop
        leave
        ret
_GLOBAL__sub_I_main:
        push    rbp
        mov     rbp, rsp
        mov     esi, 65535
        mov     edi, 1
        call    __static_initialization_and_destruction_0(int, int)
        pop     rbp
        ret

 

posted @ 2019-11-22 19:25  zzfx  阅读(211)  评论(0编辑  收藏  举报