- Expert C++
- Vardan Grigoryan Shunguang Wu
- 103字
- 2021-06-24 16:34:06
Implicit instantiations
When a function is called, the definition of that function needs to exist. If this function has not been explicitly instantiated, an implicit instantiation approach is reached, in which the list of template arguments need to be either explicitly supplied or deduced from the context. Part A of the following program provides some examples of the implicit instantiation of app_max() in this catalog:
//ch4_2_func_template_implicit_inst.cpp
#include <iostream>
template <class T>
T app_max (T a, T b) { return (a>b?a:b); }
using namespace std;
int main(){
//Part A: implicit instantiation in an explicit way
cout << app_max<int>(5, 8) << endl; //line A
cout << app_max<float>(5.0, 8.0) << endl; //line B
cout << app_max<int>(5.0, 8) << endl; //Line C
cout << app_max<double>(5.0, 8) << endl; //Line D
//Part B: implicit instantiation in an argument deduction way
cout << app_max(5, 8) << endl; //line E
cout << app_max(5.0f, 8.0f) << endl; //line F
//Part C: implicit instantiation in a confuse way
//cout<<app_max(5, 8.0)<<endl; //line G
return 0;
}
The implicit instantiations of lines A, B, C, and D are int app_max<int>(int,int), float app_max<float>(float, float>), int app_max<int>(int,int), and double app_max<double>(double, double), respectively.
推薦閱讀
- Bootstrap Site Blueprints Volume II
- R語言數(shù)據(jù)分析從入門到精通
- ASP.NET動(dòng)態(tài)網(wǎng)頁(yè)設(shè)計(jì)教程(第三版)
- Java Web程序設(shè)計(jì)
- Scala編程實(shí)戰(zhàn)(原書第2版)
- Java Fundamentals
- Building Serverless Web Applications
- WCF全面解析
- MySQL數(shù)據(jù)庫(kù)教程(視頻指導(dǎo)版)
- SQL Server 2012數(shù)據(jù)庫(kù)管理與開發(fā)(慕課版)
- Building an E-Commerce Application with MEAN
- Mastering Android Application Development
- Flask Framework Cookbook
- 深入理解OSGi:Equinox原理、應(yīng)用與最佳實(shí)踐
- Mastering CSS