模板化子类的模板化函数

时间:2014-05-04 21:20:58

标签: 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() {
    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);

1 个答案:

答案 0 :(得分:0)

我留下这个答案主要是因为评论。问题显然是postedanswered

但是根据经验,对于模板演绎,根据我的经验,使用clang作为替换/替代,它有更好的错误消息用于此类问题(例如为什么模板演绎失败)。

相关问题