官术网_书友最值得收藏!

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.

From the compiler's point of view, there are several ways to do template argument deduction deduction from a function call, deduction from a type, auto type deduction, and non-deduced contexts [4]. However, from a programmer's point of view, you should never write fancy code to ill-use the concept of function template deduction to confuse other programmers such as line G in the previous example.
主站蜘蛛池模板: 北流市| 城固县| 泰顺县| 扎鲁特旗| 博白县| 兖州市| 台东县| 通辽市| 阿坝县| 建昌县| 东平县| 东阳市| 龙游县| 怀集县| 滁州市| 临汾市| 长寿区| 禹城市| 都昌县| 军事| 阿勒泰市| 宜兰市| 建德市| 廊坊市| 新邵县| 东乡县| 齐齐哈尔市| 永康市| 永新县| 定兴县| 齐河县| 嘉禾县| 徐闻县| 台东县| 右玉县| 双城市| 乳山市| 高要市| 贺兰县| 阿尔山市| 娄底市|