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

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.
主站蜘蛛池模板: 顺昌县| 嫩江县| 侯马市| 黑河市| 南召县| 平果县| 武冈市| 海门市| 集安市| 裕民县| 保康县| 孝感市| 繁峙县| 仁布县| 岳池县| 建始县| 突泉县| 九寨沟县| 静乐县| 柳州市| 阿拉善右旗| 汉沽区| 海宁市| 曲阳县| 南召县| 屯昌县| 无极县| 普兰店市| 石门县| 昌宁县| 保定市| 芒康县| 阿图什市| 呼和浩特市| 商城县| 雷山县| 循化| 大姚县| 华阴市| 剑川县| 巢湖市|