为什么C ++模板类型匹配不检索引用限定符'&'?

时间:2017-02-24 03:16:19

标签: c++ templates types reference match

我有以下计划:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:background="@color/white_pure">

我希望这是因为#include<stdio.h> template<class T> void f(T t) { t += 1; } template<class T> void g(T &t) { t += 10; } int main() { int n=0; int&i=n; f(i); g(i); printf("%d\n",n); return 0; } 是对i的引用,所以我希望模板函数n应该为模板类型f获取int&。但事实上它没有。该计划的输出为T,而不是我预期的10

所以我的问题是,对于11,为什么f匹配T而不是int变量int&?这背后的规则是什么?

感谢。

0 个答案:

没有答案