Reputation: 179
I have the following function template:
template <typename K, typename V>
void f(std::initializer_list<std::pair<const K, V>> il)
{
//...
}
I call the function as follows:
f({std::pair<const int,int>(1,2), std::pair<const int,int>(3,4)}); //(a)
and it works fine.
However, if I try and call it as follows:
f({{1,2}, {3,4}}); //(b)
it is not able to deduce the correct type, and I get a compilation error along the lines of:
'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>>)'
If I call it as follows:
f({std::pair<const int,int>(1,2), {3,4}}); //(c)
the type deduction works, but if I try and call it as follows:
f({std::make_pair(1,2), {3,4}}); //(d)
I get the same compilation error as previously.
My question is:
Why does template type deduction work in (c) but not in (d)?
(Compiler is gcc v4.6.3, with flag -std=c++11)
I have looked at similar, older SO posts but they didn't appear to quite answer this question.
Upvotes: 5
Views: 137