Templated initialization_list - why does lookup fail
-
27-10-2019 - |
Question
Given
#include <utility>
template <typename T1, typename T2, typename T3>
void foo(std::initializer_list<std::pair<T1, T2>> _a, std::initializer_list<T3> _b) {
/* ... */
}
int main() {
foo({{1,2},{3,4}},{1,2,3,4,5});
}
why does gcc 4.6 produce the error
error: no matching function for call to ‘foo(<brace-enclosed initializer list>, <brace-enclosed initializer list>)'
note: candidate is
note: template<class T1, class T2, class T3> void foo(std::initializer_list<std::pair<T1, T2> >, std::initializer_list<T3>)
Update Now works when
template <typename T1, typename T2>
std::pair<T1, T2> p(const T1& _1, const T2& _2) {
return std::make_pair(_1, _2);
}
is added and {{1,2},{3,4}}
is replaced with {p(1,2), p(3,4)}
.
Solution
What you're trying to do is basically chained conversion/invocation, which is not allowed in C++.
First you want to convert this,
{{1,2},{3,4}}
into this (while deducing the type argument),
{std::pair<int,int>{1,2},std::pair<int,int>{3,4}}
which then into std::initializer_list
. It is not allowed.
You've to do one conversion yourself:
foo( {std::pair<int,int>{1,2},std::pair<int,int>{3,4}}, {1,2,3,4,5});
then it will compile : http://ideone.com/bRJqV
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow