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

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.

主站蜘蛛池模板: 霸州市| 城固县| 南安市| 柳州市| 南通市| 陆川县| 洛浦县| 南安市| 大悟县| 凤庆县| 乌鲁木齐县| 六盘水市| 齐齐哈尔市| 临潭县| 宿迁市| 湖南省| 江北区| 瑞昌市| 南漳县| 长葛市| 铅山县| 镇赉县| 绥化市| 高阳县| 湟中县| 通州区| 嵩明县| 万载县| 葫芦岛市| 石楼县| 保德县| 莱州市| 永康市| 文安县| 鹿邑县| 安吉县| 甘德县| 江北区| 体育| 邓州市| 屏东市|