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

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.

主站蜘蛛池模板: 五台县| 保德县| 宜阳县| 仁化县| 安徽省| 吐鲁番市| 武威市| 泰宁县| 清镇市| 呼玛县| 阳谷县| 南汇区| 台南市| 贵溪市| 井研县| 南安市| 大石桥市| 四川省| 清原| 阿拉善右旗| 林芝县| 互助| 江源县| 绥棱县| 泗阳县| 常德市| 华蓥市| 六枝特区| 乌什县| 图木舒克市| 英超| 濮阳市| 东莞市| 龙州县| 池州市| 汾西县| 日土县| 两当县| 兴业县| 新乡县| 太原市|