cant deduce type in function template

Hi, in this code when I'm trying to pass this deeply nested class to function it somehow fails to deduce template arguments that I have provided when I created my object. If I manually type in all the correct arguments than it works, also works if I let the function deduce only the last argument.

Why are all the template arguments not being deduced automatically?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

template<typename T>
class OUT {
public:
	template<int N>
	class MID {
	public:
		template<int U>
		class BASE {
		public:
			virtual void f() { std::cout << "inside BASE\nU = " << U << "\n"; }
		};
	};
};

template<typename T, int N, int U>      //these bad boys are not being deduced
void case1(typename OUT<T>::template MID<N>::template BASE<U>& p)
{
	p.f();
}

int main()
{
    OUT<int>::MID<5>::BASE<6> elem;
	
    case1<int,5,6>(elem);   //works
    case1<int,5>(elem);     //works
    case1<int>(elem);       //doesnt work
    case1(elem);            //doesnt work
}
Last edited on
IS:
A given type P can be composed from a number of other types, templates, and non-type values ...

In most cases, the types, templates, and non-type values that are used to compose P participate in template argument deduction. That is, they may be used to determine the value of a template argument, and the value so determined must be consistent with the values determined elsewhere. In certain contexts, however, the value does not participate in type deduction, but instead uses the values of template arguments that were either deduced elsewhere or explicitly specified. If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails ...

The non-deduced contexts are:
. The nested-name-specifier of a type that was specified using a qualified-id.
...


More information: see 'Non-deduced contexts' in http://en.cppreference.com/w/cpp/language/template_argument_deduction
Tnx a lot man, the rule 1 fits my problem perfectly! :)
Topic archived. No new replies allowed.