- 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.
- Python爬蟲開發(fā):從入門到實(shí)戰(zhàn)(微課版)
- PaaS程序設(shè)計
- C#程序設(shè)計基礎(chǔ):教程、實(shí)驗(yàn)、習(xí)題
- 量化金融R語言高級教程
- 軟件測試技術(shù)指南
- Essential C++(中文版)
- 零基礎(chǔ)學(xué)C++(升級版)
- Get Your Hands Dirty on Clean Architecture
- AngularJS UI Development
- HTML并不簡單:Web前端開發(fā)精進(jìn)秘籍
- HTML5程序設(shè)計基礎(chǔ)教程
- Zend Framework 2 Cookbook
- 劍指大數(shù)據(jù):企業(yè)級電商數(shù)據(jù)倉庫項(xiàng)目實(shí)戰(zhàn)(精華版)
- Spring MVC Cookbook
- Python數(shù)據(jù)科學(xué)實(shí)戰(zhàn)