Nested template and parameter deducing

c++, gcc, templates

Solution

Although it seems like a simple deduction, what you are wanting the compiler to do would actually be quite complicated and slow to do in general, and it isn't supported by C++.

One way around this is to create another non-nested class that has all the template parameters in one place. You can then make this appear to be a nested class by deriving from it:

template<int a,int b> struct A_B {
  /* define your class here */
};

template<int a> struct A
{
    template<int b> struct B : A_B<a,b> {/*nothing here*/};
};

template<int a, int b> void test(A_B<a,b> param) { }

int main()
{
    A<1>::B<2> b;

    test<1,2>(b); // works
    test(b);      // works too
}

C++11 also supports template aliasing, which makes this a little cleaner, although it isn't widely supported yet:

template<int a> struct A
{
    template<int b> using B = A_B<a,b>;
};

This question is closely related:

Workaround for non-deduced context

The answers provided there are useful for your situation as well. If you can make your function be a friend, then you can do this:

template<int a> struct A
{
    template <int b>
    struct B
    {
    };

    template <int b>
    friend void test(B<b> param)
    {
    }
};

Problem

Possible Duplicate: Workaround for non-deduced context GCC cannot deduce parameters for this 'simple' function. Is there any way to help the compiler a bit? ``` template<int a> struct A { template<int b> struct B { }; }; template<int a, int b> void test(typename A<a>::template B<b> param) { } int main() { A<1>::B<2> b; test<1,2>(b); // works test(b); // doesn't work } ``` error message from GCC 4.7.1: ``` test.cpp: In function 'int main()': test.cpp:15:8: error: no matching function for call to 'test(A<1>::B<2>&)' test.cpp:15:8: note: candidate is: test.cpp:8:29: note: template<int a, int b> void test(typename A<a>::B<b>) test.cpp:8:29: note: template argument deduction/substitution failed: test.cpp:15:8: note: couldn't deduce template parameter 'a' ```

Original source

Related problems