我在尝试弄清楚为什么gcc无法推断出以下代码中的模板参数时遇到了一些麻烦:
template <int N>
struct A {
template <int M>
struct B {
};
};
template <int N, int M>
void function(typename A<N>::template B<M> &b) {
// do stuff
}
int main() {
A<1>::B<2> b;
function(b);
}
错误:
$ g++ -std=c++11 -std=gnu++11 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:19:12: error: no matching function for call to ‘function(A<1>::B<2>&)’
function(b);
^
test.cpp:19:12: note: candidate is:
test.cpp:13:6: note: template<int N, int M> void function(typename A<N>::B<M>&)
void function(typename A<N>::template B<M> &b) {
^
test.cpp:13:6: note: template argument deduction/substitution failed:
test.cpp:19:12: note: couldn't deduce template parameter ‘N’
function(b);