The answer is that you cannot do this specialization. This is not a syntax error, but simply something that cannot be implemented. You should see template specialization a bit like function overloading. The compiler must accept the type argument in the workplace, look at the available specializations, find matches, and choose the best (most specialized) one. The problem with your example is that the βfind a matchβ step cannot be implemented with this specialization. The compiler can expect that "nested_type" will be anything, not necessarily a unique type (as in your example), it can also be a nested typedef, for example. Moreover, the compiler cannot predict that it already sees all the specializations of the template βyβ, so even if nested_type is a unique type embedded in y (a common template), it can be a nested typedef in the upcoming template specialization declaration for the template βyβ .
As with function overloading and the matching algorithm used there, the compiler is limited in its ability to infer a type, and what are the restrictions that it can make. If you have a specialization for x<int> and then use x<int> , the match is trivial, no deduction is required, no assumptions are required. If you have a specialization like x<T*> and then use x<int*> , the match is easy, T can be inferred as int . If you have a specialization of type x< y<T>::type > and then use any version of x, how should the compiler output T from y :: type? This would have to replace T in y with all possible types that exist around the world to see if there are any that lead to the corresponding nested type. This is an unreasonable expectation, and therefore the possibility of deriving C ++ template types stops here. Very often, in order to know whether one should expect the compiler to be able to solve something, just put it in your place and see if it is even possible remotely (the answer is usually clear).
source share