- Expert C++
- Vardan Grigoryan Shunguang Wu
- 156字
- 2021-06-24 16:34:06
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.
- Raspberry Pi for Python Programmers Cookbook(Second Edition)
- Java編程指南:基礎(chǔ)知識(shí)、類庫(kù)應(yīng)用及案例設(shè)計(jì)
- Banana Pi Cookbook
- 匯編語言程序設(shè)計(jì)(第3版)
- Kotlin Standard Library Cookbook
- Learning Data Mining with R
- 微信小程序項(xiàng)目開發(fā)實(shí)戰(zhàn)
- 小學(xué)生C++創(chuàng)意編程(視頻教學(xué)版)
- 劍指MySQL:架構(gòu)、調(diào)優(yōu)與運(yùn)維
- C++新經(jīng)典
- Building RESTful Python Web Services
- MySQL從入門到精通(軟件開發(fā)視頻大講堂)
- iOS自動(dòng)化測(cè)試實(shí)戰(zhàn):基于Appium、Python與Pytest
- 打開Go語言之門:入門、實(shí)戰(zhàn)與進(jìn)階
- Mastering Docker