0

I'm having a bit of trouble trying to figure out why gcc cannot deduce the template arguments in the following code:

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);
}

Error:

$ 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 Answers1

0

I leave this answer mainly because of the comments. The question has apparently been previously posted and answered.

But as a rule of thumb, for template deduction, in my experience use clang as replacement/alternative, it has much better error messages for such issues (like why template deduction failed).

Community
  • 1
  • 1
Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
  • This was a mistake I made while transferring the code. The actual code that was run to produce the error output is "correct" in this sense. –  May 04 '14 at 21:31
  • 1
    Ok, still I would suggest using clang to get a 'second opinion' so to speak. – Janick Bernet May 04 '14 at 21:32
  • Thanks, downloading right now –  May 04 '14 at 21:33
  • The clang output was pretty much the same; it said it could not infer the template parameter –  May 04 '14 at 21:35
  • 3
    This link explains why you cannot do exactly what you want: http://stackoverflow.com/questions/12640808/nested-template-and-parameter-deducing – vsoftco May 04 '14 at 21:40