C++ 模板化子类上的模板化函数

C++ 模板化子类上的模板化函数,c++,templates,c++11,C++,Templates,C++11,我在试图找出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() {

我在试图找出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);
}
模板
结构A{
样板
结构B{
};
};
样板
void函数(类型名A::模板B&B){
//做事
}
int main(){
A::B;
职能(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);
$g++-std=c++11-std=gnu++11 test.cpp
test.cpp:在函数“int main()”中:
test.cpp:19:12:错误:调用“function(A::B&)”时没有匹配的函数
职能(b);
^
测试。cpp:19:12:注:候选人为:
test.cpp:13:6:注意:模板void函数(typename A::B&)
void函数(类型名A::模板B&B){
^
test.cpp:13:6:注意:模板参数扣除/替换失败:
test.cpp:19:12:注意:无法推断模板参数“N”
职能(b);

我留下这个答案主要是因为这些评论。这个问题显然是以前和现在的问题


但根据经验,对于模板推断,根据我的经验,使用clang作为替换/替代,对于此类问题(如模板推断失败的原因),它有更好的错误消息。

您确定是在启用c++11选项的情况下编译的吗?a::B()--我对此总是有点不确定,但这不是最麻烦的解析吗?在
main
中,
b
是一个函数。@PhilippLenk:它现在正迅速成为最麻烦的解析对象。下面是您要寻找的:这是我在传输代码时犯的一个错误。运行以产生错误输出的实际代码是从这个意义上说“正确”。好的,我仍然建议使用clang来获得“第二个意见”。谢谢,现在下载clang输出基本相同;它说它无法推断模板参数。此链接解释了为什么您不能完全按照自己的要求做: