The consequence of code expansion caused by template instantiation (20 lines of code compiled by the compiler will die)

Source: Internet
Author: User
First, describe the configuration of a PC as follows: Operating System: Mac OS lion 10.7.3 (64 bit) CPU: Intel I5 memory: Samsung ddr3 1333 4G Compiler: xcode 4.3.2 (64 bit) the Code is as follows:

 

#include<iostream>//author:1025679612@qq.com//csdn:wind_2008_06_29#include<typeinfo>usingnamespacestd;template<        typename       FirstType,typename       SecondType,        typename       ThirdType,typename       FourthType        >struct  Doublify{    //empty !};template<int    N>struct Trouble {   typedefDoublify<                    typenameTrouble<N-1>::LongType,                    typenameTrouble<N-1>::LongType,                    typenameTrouble<N-1>::LongType,                    typenameTrouble<N-1>::LongType                    >  LongType;};template<>struct  Trouble<0>{   typedefdouble  LongType;};int main(){   Trouble<900>::LongType  value;   cout<<typeid(value).name()<<endl;}


The compilation progress of the compiler was shown above when I came back from the stool. (this is also where I clicked the compilation, and it is also there when I came back ). haha, my compiler was finally speechless.
Explanation: some people may think that the reason why the compiler compilation is so slow is the constant instantiation of trouble. Actually, it is not. Trouble can be instantiated for a maximum of 900 times. But why is the compiler slow, the reason is that the compiler uses all the time to write the name of the template after instantiation. Those names are hundreds of millions of characters, you can see in my code that there are only a few characters, but the name of the compiler will be changed ). the time for the compiler to read and write hundreds of millions of characters can be imagined. I changed the instantiation constant to 10, and the compiler compiled for 5 s, and we can see that the name given to the value by the compiler is: the compiler hopes that this name will not scare you. Let's think about it again, when the number is 900, what is the Compilation Time? It must have been the day I saw Marx (note that the time here is the exponential growth relationship). What about the symbol length? It may be several hundred million characters, haha. if you want to test vs, I suggest you change 900 to 20. 900 is too large, VS is 32-bit, and the memory space is not enough, during compilation, an error will definitely occur. You can try it by yourself when you change it to 20.

 

If you want to reprint it, please indicate the source. Thank you.

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.