函數(shù)模板,不是模板類(lèi),與函數(shù)重載的情況.以及函數(shù)模板的機(jī)制研究

這里有:
1.函數(shù)模板的基本調(diào)用
2.當(dāng)函數(shù)模板和函數(shù)重載在一起的時(shí)候,先調(diào)用普通函數(shù)還是函數(shù)模板
2.函數(shù)模板在c++編譯器中的匯編形式

#include <iostream>
using namespace std;


int Max(int a,int b){
    cout<<"int Max(int a,int b)"<<endl;
    
    return a>b?a:b;
}


template<typename T>
T Max(T a,T b){
    
    cout<<"T Max(T a,T b)"<<endl;
    
    return a>b?a:b;
}

template<typename T>
T Max(T a,T b,T c){
    cout<<""<<endl;
    return Max(Max(a, b), c);
}

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    
    int a =1;
    int b = 1;
    
    cout<<Max(a, b)<<endl; //當(dāng)函數(shù)模板和普通函數(shù)都符合調(diào)用時(shí),優(yōu)先調(diào)用普通函數(shù)
    
    cout<<Max<>(a,b)<<endl;//若顯示使用函數(shù)模板,則使用<>類(lèi)型 類(lèi)型列表
    
    cout<<Max(3.0, 4.0)<<endl;//如果函數(shù)模板產(chǎn)生更好的匹配,則使用函數(shù)模板
    
    cout<<Max(5.0,6.0,7.0)<<endl;//一個(gè)類(lèi)中,同名函數(shù)重載 調(diào)用的是模板函數(shù)
    
    cout<<Max('a', 100)<<endl;//調(diào)用普通函數(shù),可以隱式的類(lèi)型轉(zhuǎn)換.//這里不調(diào)用模板函數(shù),是因?yàn)槟0搴瘮?shù)要求參數(shù)是兩個(gè)類(lèi)型一樣的參數(shù),模板函數(shù)會(huì)進(jìn)行嚴(yán)格的類(lèi)型匹配檢查.
    
    
    

    return 0;
}

使用g++ -S main.cpp -0 1.s 命令 ,輸出匯編源碼

    .section    __TEXT,__text,regular,pure_instructions
    .macosx_version_min 10, 11
    .globl  __Z3Maxii
    .align  4, 0x90
__Z3Maxii:                              ## @_Z3Maxii
    .cfi_startproc
## BB#0:
    pushq   %rbp
Ltmp0:
    .cfi_def_cfa_offset 16
Ltmp1:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp2:
    .cfi_def_cfa_register %rbp
    subq    $48, %rsp
    movq    __ZNSt3__14coutE@GOTPCREL(%rip), %rax
    leaq    L_.str(%rip), %rcx
    movl    %edi, -20(%rbp)
    movl    %esi, -24(%rbp)
    movq    %rax, %rdi
    movq    %rcx, %rsi
    callq   __ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc
    leaq    __ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_(%rip), %rcx
    movq    %rax, -8(%rbp)
    movq    %rcx, -16(%rbp)
    movq    -8(%rbp), %rdi
    callq   *-16(%rbp)
    movl    -20(%rbp), %edx
    cmpl    -24(%rbp), %edx
    movq    %rax, -32(%rbp)         ## 8-byte Spill
    jle LBB0_2
## BB#1:
    movl    -20(%rbp), %eax
    movl    %eax, -36(%rbp)         ## 4-byte Spill
    jmp LBB0_3
LBB0_2:
    movl    -24(%rbp), %eax
    movl    %eax, -36(%rbp)         ## 4-byte Spill
LBB0_3:
    movl    -36(%rbp), %eax         ## 4-byte Reload
    addq    $48, %rsp
    popq    %rbp
    retq
    .cfi_endproc

    .section    __TEXT,__textcoal_nt,coalesced,pure_instructions
    .globl  __ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc
    .weak_def_can_be_hidden __ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc
    .align  4, 0x90
__ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc: ## @_ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc
    .cfi_startproc
## BB#0:
    pushq   %rbp
Ltmp3:
    .cfi_def_cfa_offset 16
Ltmp4:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp5:
    .cfi_def_cfa_register %rbp
    subq    $32, %rsp
    movq    %rdi, -8(%rbp)
    movq    %rsi, -16(%rbp)
    movq    -8(%rbp), %rdi
    movq    -16(%rbp), %rsi
    movq    -16(%rbp), %rax
    movq    %rdi, -24(%rbp)         ## 8-byte Spill
    movq    %rax, %rdi
    movq    %rsi, -32(%rbp)         ## 8-byte Spill
    callq   __ZNSt3__111char_traitsIcE6lengthEPKc
    movq    -24(%rbp), %rdi         ## 8-byte Reload
    movq    -32(%rbp), %rsi         ## 8-byte Reload
    movq    %rax, %rdx
    callq   __ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m
    addq    $32, %rsp
    popq    %rbp
    retq
    .cfi_endproc

    .private_extern __ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_
    .globl  __ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_
    .weak_definition    __ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_
    .align  4, 0x90
__ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_: ## @_ZNSt3__14endlIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_
Lfunc_begin0:
    .cfi_startproc
    .cfi_personality 155, ___gxx_personality_v0
    .cfi_lsda 16, Lexception0
## BB#0:
    pushq   %rbp
Ltmp14:
    .cfi_def_cfa_offset 16
Ltmp15:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp16:
    .cfi_def_cfa_register %rbp
    subq    $144, %rsp
    movq    %rdi, -72(%rbp)
    movq    %rdi, %rax
    movq    (%rdi), %rcx
    movq    -24(%rcx), %rcx
    addq    %rcx, %rdi
    movq    %rdi, -32(%rbp)
    movb    $10, -33(%rbp)
    movq    -32(%rbp), %rsi
    leaq    -48(%rbp), %rcx
    movq    %rcx, %rdi
    movq    %rax, -80(%rbp)         ## 8-byte Spill
    movq    %rcx, -88(%rbp)         ## 8-byte Spill
    callq   __ZNKSt3__18ios_base6getlocEv
    movq    -88(%rbp), %rax         ## 8-byte Reload
    movq    %rax, -24(%rbp)
Ltmp6:
    movq    __ZNSt3__15ctypeIcE2idE@GOTPCREL(%rip), %rsi
    movq    %rax, %rdi
    callq   __ZNKSt3__16locale9use_facetERNS0_2idE
Ltmp7:
    movq    %rax, -96(%rbp)         ## 8-byte Spill
    jmp LBB2_1
LBB2_1:                                 ## %_ZNSt3__19use_facetINS_5ctypeIcEEEERKT_RKNS_6localeE.exit.i
    movb    -33(%rbp), %al
    movq    -96(%rbp), %rcx         ## 8-byte Reload
    movq    %rcx, -8(%rbp)
    movb    %al, -9(%rbp)
    movq    -8(%rbp), %rdx
    movq    (%rdx), %rsi
    movq    56(%rsi), %rsi
    movsbl  -9(%rbp), %edi
Ltmp8:
    movl    %edi, -100(%rbp)        ## 4-byte Spill
    movq    %rdx, %rdi
    movl    -100(%rbp), %r8d        ## 4-byte Reload
    movq    %rsi, -112(%rbp)        ## 8-byte Spill
    movl    %r8d, %esi
    movq    -112(%rbp), %rdx        ## 8-byte Reload
    callq   *%rdx
Ltmp9:
    movb    %al, -113(%rbp)         ## 1-byte Spill
    jmp LBB2_5
LBB2_2:
Ltmp10:


...代碼太多就不展示了
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *面試心聲:其實(shí)這些題本人都沒(méi)怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來(lái)就是把...
    Dove_iOS閱讀 27,655評(píng)論 30 472
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類(lèi)相關(guān)的語(yǔ)法,內(nèi)部類(lèi)的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線(xiàn)程的語(yǔ)...
    子非魚(yú)_t_閱讀 34,834評(píng)論 18 399
  • 一、溫故而知新 1. 內(nèi)存不夠怎么辦 內(nèi)存簡(jiǎn)單分配策略的問(wèn)題地址空間不隔離內(nèi)存使用效率低程序運(yùn)行的地址不確定 關(guān)于...
    SeanCST閱讀 8,141評(píng)論 0 27
  • 前言 把《C++ Primer》[https://book.douban.com/subject/25708312...
    尤汐Yogy閱讀 9,691評(píng)論 1 51
  • 每個(gè)人的生活都是一部直播的電視劇,以前總是聽(tīng)我媽說(shuō)起:“家家有本難念的經(jīng)”,直到現(xiàn)在深有體會(huì)。 一個(gè)人想要的...
    合肥李風(fēng)麗閱讀 244評(píng)論 1 0

友情鏈接更多精彩內(nèi)容