- 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.
- Mastering Concurrency in Go
- Scratch 3.0少兒編程與邏輯思維訓練
- 高級C/C++編譯技術(典藏版)
- C++ 從入門到項目實踐(超值版)
- 軟件測試技術指南
- SQL Server 2016數據庫應用與開發
- 全棧自動化測試實戰:基于TestNG、HttpClient、Selenium和Appium
- Java Web開發詳解
- Python極簡講義:一本書入門數據分析與機器學習
- Procedural Content Generation for C++ Game Development
- Visual Basic 6.0程序設計實驗教程
- Orleans:構建高性能分布式Actor服務
- Illustrator CS6設計與應用任務教程
- Web編程基礎:HTML5、CSS3、JavaScript(第2版)
- Scrapy網絡爬蟲實戰