我看到這個代碼在互聯網上:爲什麼不會這個模板函數編譯C++
template <class T, class S>
T f(T& a, T& b, S c) {
T t1, t2, t3;
if (a==b) return t1;
if (c>0) return t2;
return t3;
}
隨後這些調用:
f('c', 5, 5); // why this doesn't compile?
f(3, 4, 6.0); // why does this compile?
第一個沒有編制,而第二確實..爲什麼?
@RSahu燁,編輯。 –
你確定你想在傳遞文字時使用引用嗎? – Matt
模板規範預計參數'a'和'b'應該是相同的類型。他們不是第一個,但他們在第二個。 – andand