官术网_书友最值得收藏!

Instantiation

Since we may potentially have an infinite number of types and classes, the concept of function templates not only saves space in the source code file but also makes code easier to read and maintain. However, compared to writing separate functions or classes for the different data types that are used in our applications, it does not produce smaller object code. For instance, consider a program using a float and int version of app_max():

cout << app_max<int>(3,5) << endl;
cout << app_max<float>(3.0f,5.0f) << endl;

The compiler will generate two new functions in the object file, as follows:

int app_max<int> ( int a, int b) {
return (a>b?a:b);
}

float app_max<float> (float a, float b) {
return (a>b?a:b);
}

This process of creating a new definition of a function from a function template declaration is called template instantiation. During this instantiation process, the compiler determines the template arguments and generates actual functional code on demand for your application. Typically, there are three forms: explicit instantiations, implicit instantiations, and template deductions. In the next sections, let's discuss each form.

主站蜘蛛池模板: 遂溪县| 西华县| 商南县| 海原县| 泊头市| 富裕县| 楚雄市| 乌鲁木齐县| 藁城市| 永寿县| 伊川县| 阿巴嘎旗| 平山县| 金沙县| 夏邑县| 山阴县| 景东| 曲麻莱县| 定结县| 慈溪市| 永善县| 岑巩县| 磐石市| 中方县| 山东| 江永县| 临汾市| 开阳县| 图们市| 双柏县| 运城市| 高碑店市| 宁阳县| 革吉县| 黔南| 乌拉特中旗| 嘉义县| 建阳市| 罗江县| 固原市| 北海市|