When C ++ calls a template with the same real parameter multiple times, the compiler will list the code several times.

Source: Internet
Author: User

Question:


Template <typename T> void fun (T num ){};
If you call
Fun (double (1.23 ));
Fun (int (1 ));
The compiler will enumerate fun (double num) and fun (int num)
But I want to ask if you call fun (int) twice)
Fun (int (1 ));
Fun (int (2 ));
The compiler will actually list two fun (int), or one ???? I am very confused.
Please kindly advise.

 

 

[Cpp]
# Include <iostream>
Using namespace std;
Template <typename T>
Void fun (T num)
{
Static int cnt = 0;
Cnt ++;
Cout <"cnt =" <cnt <"\ t" <"num =" <num <endl;
}
Int main ()
{
Int a = 1;
Fun ();
Int B = 2;
Fun (B );
Double c = 2.23;
Fun (c );
Return 0;
}

# Include <iostream>
Using namespace std;
Template <typename T>
Void fun (T num)
{
Static int cnt = 0;
Cnt ++;
Cout <"cnt =" <cnt <"\ t" <"num =" <num <endl;
}
Int main ()
{
Int a = 1;
Fun ();
Int B = 2;
Fun (B );
Double c = 2.23;
Fun (c );
Return 0;
}
Result:

Cnt = 1 num = 1
Cnt = 2 num = 2
Cnt = 1 num = 2.23

Conclusion: only once


 

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.