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

  • 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.

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.
主站蜘蛛池模板: 卢龙县| 桂阳县| 贡嘎县| 滁州市| 探索| 兴义市| 涟水县| 仁寿县| 吐鲁番市| 申扎县| 潜江市| 英超| 呼玛县| 中方县| 延安市| 南城县| 尉犁县| 娱乐| 汾阳市| 黄陵县| 彭州市| 遵义市| 武功县| 依兰县| 崇明县| 科尔| 都安| 隆安县| 孟州市| 仪征市| 二手房| 姚安县| 德庆县| 柳州市| 定陶县| 柏乡县| 三亚市| 安义县| 大冶市| 建平县| 巴里|