- Expert C++
- Vardan Grigoryan Shunguang Wu
- 232字
- 2021-06-24 16:34:06
Deduction
When you call a template function, the compiler needs to figure out the template arguments first, even if not every template argument is specified. Most of the time, it will deduce the missing template arguments from the function arguments. For example, in part B of the preceding function, when you call app_max(5, 8) in line E, the compiler deduces the template argument as an int type, (int app_max<int>(int,int)), because the input parameters, 5 and 8, are integers. Similarly, line F will be deduced as a float type, that is, float app_max<float>(float,float).
However, what happens if there is confusion during instantiation? For instance, in the commented out line for G of the previous program, depending on the compiler, it might call app_max<double>(double, double), app_max<int>(int, int), or just give a compile error message. The best way to help the compiler deduce the type is to call the function template by giving a template argument explicitly. In this case, if we call app_max<double>(5, 8.0), any confusion will be resolved.
- Java系統分析與架構設計
- TensorFlow Lite移動端深度學習
- 從程序員到架構師:大數據量、緩存、高并發、微服務、多團隊協同等核心場景實戰
- Java設計模式及實踐
- JavaScript動態網頁開發詳解
- 精通Python自然語言處理
- 執劍而舞:用代碼創作藝術
- Unity&VR游戲美術設計實戰
- UI設計全書(全彩)
- C++ System Programming Cookbook
- Practical Predictive Analytics
- C語言程序設計教程
- Learn Linux Quickly
- HTML5 and CSS3:Building Responsive Websites
- Python程序員面試算法寶典