說的對象我有這樣的功能:轉換算符對類型功能
template <typename T>
void foo(function<T(const T&, const T&)> op, const T& lhs, const T& rhs) {
cout << op(lhs, rhs) << endl;
}
這是legal code:
function<int(const int&, const int&)> op = plus<int>();
foo(op, 13, 42);
但是,當我這樣做:
foo(plus<int>(), 13, 42)
我得到錯誤:
No matching function for call to
foo(std::plus<int>, int, int)
爲什麼我可以從plus<int>()
初始化function<int(const int&, const int&)>
類型的對象,但是我無法將plus<int>()
轉換成類型爲function<T(const T&, const T&)>
的參數?這是否與模板有關?
隱式類型轉換在模板參數推演期間不起作用。 – Arunmu
@Arunmu我認爲這個規則有例外:[例子](http://melpon.org/wandbox/permlink/ld8NyDotgjK7B7WT) –
@Arunmu這是它的工作原理,如果我刪除模板:http:// ideone.com/bfjy3p那麼我怎麼知道'plus()'和'function 之間的轉換是隱式的? –