我有以下功能模板:
template <typename K, typename V>
void f(std::initializer_list<std::pair<const K, V>> il)
{
//...
}
我调用函数如下:
f({std::pair<const int,int>(1,2), std::pair<const int,int>(3,4)}); //(a)
它工作正常。
但是,如果我尝试如下调用它:
f({{1,2}, {3,4}}); //(b)
它无法推断出正确的类型,并且出现以下编译错误:
'no matching function for call to 'f(<brace-enclose initializer list>)
note candidate is:
note: template <class K, class V> void f(std::initializer_list<std::pair<const K, V>>)'
如果我这样称呼它:
f({std::pair<const int,int>(1,2), {3,4}}); //(c)
类型推导有效,但如果我尝试如下调用它:
f({std::make_pair(1,2), {3,4}}); //(d)
我得到与以前相同的编译错误。
我的问题是:
为什么模板类型推导在 (c) 中有效,但在 (d) 中无效?
(编译器是 gcc v4.6.3,带有标志 -std=c++11)
我看过类似的、较旧的 SO 帖子,但他们似乎并没有完全回答这个问题。