- 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.
- ASP.NET Web API:Build RESTful web applications and services on the .NET framework
- JavaScript:Functional Programming for JavaScript Developers
- 零基礎(chǔ)學(xué)MQL:基于EA的自動(dòng)化交易編程
- Python 3破冰人工智能:從入門到實(shí)戰(zhàn)
- 微信公眾平臺(tái)開發(fā):從零基礎(chǔ)到ThinkPHP5高性能框架實(shí)踐
- Linux操作系統(tǒng)基礎(chǔ)案例教程
- 數(shù)據(jù)結(jié)構(gòu)案例教程(C/C++版)
- Java Web從入門到精通(第3版)
- Go語言底層原理剖析
- Swift語言實(shí)戰(zhàn)晉級(jí)
- Hadoop 2.X HDFS源碼剖析
- PHP+MySQL動(dòng)態(tài)網(wǎng)站開發(fā)從入門到精通(視頻教學(xué)版)
- ASP.NET開發(fā)寶典
- 體驗(yàn)之道:從需求到實(shí)踐的用戶體驗(yàn)實(shí)戰(zhàn)
- 一步一步學(xué)Spring Boot:微服務(wù)項(xiàng)目實(shí)戰(zhàn)(第2版)