Analysis of the template mechanism of C + + compiler function--the essence of function template

Source: Internet
Author: User

Think: Why function templates can be put together with function overloading. How does the C + + compiler provide a mechanism for function templates?

Demo 1

#include <cstdio> #include <iostream>using namespace std;//1.cpp//g++-S 1.cpp-  o 1.stemplate < TypeName T>void Myswap (t &a, t &b) {T c = 0;c = A;a = B;b = c;cout << "Hello ...." I am a template function welcome calll me "<< Endl;} int main () {{int x = ten; int y = 20;myswap<int> (x, y);//1 function template display type call printf ("x:%d y:%d \ n", x, y);} {Char a = ' a '; char b = ' B ';myswap<char> (A, b);//1 function template display type call printf ("a:%c b:%c \ n", A, b);} return 0;}

Compile the demo 1 into a compilation file to view:

. File "1.cpp". Lcomm __zstl8__ioinit,1,1.def___main;. SCl2;. Type32;. Endef.section. Rdata, "Dr" Lc0:.ascii "x:%d y:%d \12\0" Lc1:.ascii "a:%c b:%c \12\0". def___gxx_personality_sj0;. SCl2;. Type32;. Endef.def__unwind_sjlj_register;. SCl2;. Type32;. Endef.def__unwind_sjlj_unregister;. SCl2;. Type32;. Endef.text.globl_main.def_main;. SCl2;. Type32;. Endef_main:pushl%ebpmovl%esp,%ebppushl%edipushl%esipushl%ebxandl$-16,%espsubl$96,%espmovl$___gxx_personality_ Sj0,%esp movl$llsda959, (%ESP) leal60 (%ESP),%EAXMOVL%EBP, (%eax) Movl$l5,%edxmovl%edx, 4 (%EAX) Movl%esp, 8 (%EAX) Leal28 (%ESP),%eaxmovl%eax, (%ESP) call__unwind_sjlj_registercall___mainmovl$10, (%ESP) movl$20, (%ESP) leal88 (% ESP),%eaxmovl%eax, 4 (%ESP) leal92 (%ESP),%eaxmovl%eax, (%ESP) movl$1, (%ESP) <span style= "color: #ff0000;" >call__z6myswapiievrt_s1_//117</span>movl88 (%ESP),%edxmovl92 (%ESP),%eaxmovl%edx, 8 (%ESP) movl%eax , 4 (%ESP) movl$lc0, (%ESP) call_printfmovb$97, (%ESP) movb$98, (%ESP) leal86 (%ESP),%eaxmovl%eax, 4 (%esp) leal87 (%ESP),%eaxmovl%eax, (%ESP) movl$2, (%ESP) <span style= "color: #ff0000;" >call__z6myswapicevrt_s1_//145</span>movb86 (%ESP),%almovsbl%al,%edxmovb87 (%ESP),%almovsbl%al,% Eaxmovl%edx, 8 (%ESP) Movl%eax, 4 (%ESP) Movl$lc1, (%ESP) call_printfmovl$0,%eaxmovl%eax, (%ESP) jmpl8l5:movl36 (%ESP), %edxmovl32 (%ESP),%eaxtestl%eax,%eaxjel6cmpl$1,%eaxjel7.word0x0b0fl6:movl%edx,%eaxmovl%eax, (%ESP) movl$-1, 32 (% ESP) Call__unwind_sjlj_resumel7:movl%edx,%eaxmovl%eax, (%ESP) movl$-1, (%ESP) call__unwind_sjlj_resumel8:leal28 (% ESP),%eaxmovl%eax, (%ESP) call__unwind_sjlj_unregistermovl24 (%ESP),%eaxleal-12 (%EBP),%esppopl%ebxpopl%esipopl% Edipopl%ebpret.section.gcc_except_table, "W" llsda959:.byte0xff.byte0xff.byte0x1.uleb128 llsdacse959-llsdacsb959llsdacsb959:.uleb128 0.uleb128 0.uleb128 0x1.uleb128 0llsdacse959:.text.section. rdata, "Dr". Align 4lc2:.ascii "hello ... \316\322\312\307\304\243\260\345\272\257\312\375 \273\266\323\255 calll \316\322\0". section.text$_z6myswapiievrt_s1_, "x". Linkonce discard.globl__Z6myswapIiEvRT_S1_.def__Z6myswapIiEvRT_S1_;. SCl2;. Type32;. Endef<span style= "color: #ff0000;" >__z6myswapiievrt_s1_://117</span>pushl%ebpmovl%esp,%ebpsubl$40,%espmovl$0, -12 (%EBP) Movl8 (%EBP),% EAXMOVL (%eax),%eaxmovl%eax, -12 (%EBP) movl12 (%EBP),%EAXMOVL (%eax),%edxmovl8 (%EBP),%eaxmovl%edx, (%eax) movl12 (%EBP ),%eaxmovl-12 (%EBP),%edxmovl%edx, (%eax) MOVL$LC2, 4 (%ESP) Movl$__zst4cout, (%ESP) call__zstlsist11char_ Traitsiceerst13basic_ostreamict_es5_pkcmovl$__zst4endlicst11char_traitsiceerst13basic_ostreamit_t0_es6_, (%ESP) Movl%eax,%ecxcall__znsolsepfrsos_esubl$4,%espleaveret.section.text$_z6myswapicevrt_s1_, "X". linkonce discard.globl__Z6myswapIcEvRT_S1_.def__Z6myswapIcEvRT_S1_;. SCl2;. Type32;. Endef<span style= "color: #ff0000;" >__z6myswapicevrt_s1_://145</span>pushl%ebpmovl%esp,%ebpsubl$40,%espmovb$0, -9 (%EBP) Movl8 (%EBP),% Eaxmovb (%eax),%almovb%al, -9 (%EBP) movl12 (%EBP),%eaxmovb (%eax),%dlmovl8 (%EBP),%EAXMOVB%DL, (%eax) movl12 (%EBP),%eaxmovb-9 (%EBP),%DLMOVB%DL, (%eax) MOVL$LC2, 4 (%ESP) Movl$__zst4cout, (%ESP) call__zstlsist11char_ Traitsiceerst13basic_ostreamict_es5_pkcmovl$__zst4endlicst11char_traitsiceerst13basic_ostreamit_t0_es6_, (%ESP) Movl%eax,%ecxcall__znsolsepfrsos_esubl$4,%espleaveret.text.def___tcf_0; Scl3;. Type32;. Endef___tcf_0:pushl%ebpmovl%esp,%ebpsubl$8,%espmovl$__zstl8__ioinit,%ecxcall__znst8ios_ BASE4INITD1EVLEAVERET.DEF__Z41__STATIC_INITIALIZATION_AND_DESTRUCTION_0II;. Scl3;. Type32;. Endef__z41__static_initialization_and_destruction_0ii:pushl%ebpmovl%esp,%ebpsubl$24,%espcmpl$1, 8 (%EBP) jnel12cmpl$65535, (%EBP) Jnel12movl$__zstl8__ioinit,%ecxcall__znst8ios_base4initc1evmovl$___tcf_0, (%ESP) Call_ AtexitL12:leaveret.def__GLOBAL__sub_I_main;. Scl3;. Type32;. Endef__global__sub_i_main:pushl%ebpmovl%esp,%ebpsubl$24,%espmovl$65535, 4 (%ESP) movl$1, (%ESP) call__Z41__static_ Initialization_and_destruction_0iileaveret.section.ctors, "W". Align 4.long__global__sub_i_main.def__unwind_sjlj_ Resume;. SCl2;. Type32;. Endef.def_printf;. SCl2;. Type32;. endef.def__zst4endlicst11char_traitsiceerst13basic_ostreamit_t0_es6_;. SCl2;. Type32;. ENDEF.DEF__ZSTLSIST11CHAR_TRAITSICEERST13BASIC_OSTREAMICT_ES5_PKC;. SCl2;. Type32;. Endef.def__znsolsepfrsos_e;. SCl2;. Type32;. Endef.def__znst8ios_base4initd1ev;. SCl2;. Type32;. Endef.def__znst8ios_base4initc1ev;. SCl2;. Type32;. Endef.def_atexit;. SCl2;. Type32;. Endef
Observation found a phenomenon, the Myswap function template has a declaration, two definitions, this situation and I in the "why there is a function template" mentioned in the two Myswap function is very similar, the actual embodiment of the C + + implementation function template, it would need programmers to write a lot of logic the same, Parameters are different functions, but the C + + compiler helps us do this, according to the call will automatically generate these functions, which is why the function template can be put together with normal functions.

Summary: function template Mechanism conclusion:

The compiler does not process the function template into functions that can handle any class;

The compiler produces different functions from the function template through the specific type;

The compiler compiles the function template two times: compile the template code itself where it is declared, and compile the replacement code at the point where it is called.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Analysis of the template mechanism of C + + compiler function--the essence of function template

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.