View C ++ from the perspective of assembly (Template Function)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @]

If the template class defines a data type, the template function defines a function. Since it is a function, there is input data and output data. Similar to the concept of a template class, the original intention of a template function is to extract common features in function operations, shielding the differences and differences between types. We can use the following simple code to describe the problem:

int int_compare(int a, int b){return a > b ? a : b;}double double_compare(double a, double b){return a > b ? a : b;}

The above code is a piece of code with a higher latency. The biggest difference between the two functions is the difference between the input and output data types. Is there a way to block the difference between these data types? Yes. That is, the function template:

template <typename type>type compare(type a, type b){return a > b ? a : b;}

As you can see, there is no difference between a template function and a common function, but the type is abstracted to type on the function. How can we use a template function?

246:      int i_value = compare(2, 3);00401458   push        30040145A   push        20040145C   call        @ILT+10(compare) (0040100f)00401461   add         esp,800401464   mov         dword ptr [ebp-4],eax247:      double d_value = compare(2.3, 3.1);00401467   push        4008CCCCh0040146C   push        0CCCCCCCDh00401471   push        40026666h00401476   push        66666666h0040147B   call        @ILT+5(compare) (0040100a)00401480   add         esp,10h00401483   fstp        qword ptr [ebp-0Ch]248:  }

The Assembly Code shows that the address of the two compare functions is not the same. The integer compare address is 0x40100f, and the double address is 0x0040100a. This shows that the compiler generates two compare functions at the same time during compilation. Therefore, the essence of the template class is to reduce manual repetitive work while the compiler adds judgment and processing work. Unlike the template class, the template function does not need to display the parameter type of the defined function, because the type of the function can be determined from the input parameter.

What if the parameter type is class? We can give it a try. First, define the basic class:

class data{int value;public:explicit data(int m): value(m) {}~data() {}int get_value() { return value;}int operator > (data& d) {return this->get_value() > d.get_value();}};

Next, we call the Compare function:

256:      data m(4), n(2);0040148D   push        40040148F   lea         ecx,[ebp-10h]00401492   call        @ILT+40(data::data) (0040102d)00401497   mov         dword ptr [ebp-4],00040149E   push        2004014A0   lea         ecx,[ebp-14h]004014A3   call        @ILT+40(data::data) (0040102d)004014A8   mov         byte ptr [ebp-4],1257:      data p = compare(m,n);004014AC   mov         eax,dword ptr [ebp-14h]004014AF   push        eax004014B0   mov         ecx,dword ptr [ebp-10h]004014B3   push        ecx004014B4   lea         edx,[ebp-18h]004014B7   push        edx004014B8   call        @ILT+15(compare) (00401014)004014BD   add         esp,0Ch258:  }

Row 256: Data constructs two basic variables: M and N.

Row 257: we call the template function compare. The function address is 0x401014. Note that dx is the P address, that is, the address of the stack temporary variable.

To see Arithmetic Operators> overload, follow up the Compare function:

241:      return a > b ? a : b;0040212B   lea         eax,[ebp+10h]0040212E   push        eax0040212F   lea         ecx,[ebp+0Ch]00402132   call        @ILT+55(data::operator>) (0040103c)00402137   test        eax,eax00402139   je          compare+53h (00402143)0040213B   lea         ecx,[ebp+0Ch]0040213E   mov         dword ptr [ebp-18h],ecx00402141   jmp         compare+59h (00402149)00402143   lea         edx,[ebp+10h]00402146   mov         dword ptr [ebp-18h],edx00402149   mov         eax,dword ptr [ebp-18h]0040214C   mov         dword ptr [ebp-10h],eax0040214F   mov         ecx,dword ptr [ebp-10h]00402152   mov         edx,dword ptr [ecx]00402154   mov         eax,dword ptr [ebp+8]00402157   mov         dword ptr [eax],edx00402159   mov         ecx,dword ptr [ebp-14h]0040215C   or          ecx,10040215F   mov         dword ptr [ebp-14h],ecx00402162   mov         byte ptr [ebp-4],100402166   lea         ecx,[ebp+0Ch]00402169   call        @ILT+25(data::~data) (0040101e)0040216E   mov         byte ptr [ebp-4],000402172   lea         ecx,[ebp+10h]00402175   call        @ILT+25(data::~data) (0040101e)0040217A   mov         eax,dword ptr [ebp+8]

We found that the following compare template statements build a lot of Assembly statements, which are lengthy. We can briefly introduce them:

(1) Call call 0x0040103c function is to call the overload operator function, [ebp-18h] indicates whether to be copied a data or B Data

(2) Start copying the data after comparing the returned results, see 0x402157, where actions on the temporary variables [ebp-14h] and temporary variables [ebp-4] can be ignored

(3) Before the function is returned, analyze the temporary variables A and B. For details, see Code 0x402169 and code 0x402175.


(1) Make sure that your functions are correctly written before writing template functions.

(2) The priority of a template function is lower than that of a non-template function.

(3) The template function type can be a custom type or a basic type in C and C ++ languages.

(4) The use of template functions is often mixed with Arithmetic Operators of classes.

(5) the pointer part of the template function must be noted.

[Notice: The next template mainly describes the Special template and default 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: 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.