Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: class template partial specialization contains a template parameter that cannot be deduced

I'd appreciate help figuring out what going on in this problem that's come up in my code which I've reduced to the following:

typedef unsigned short ushort;

template<typename T = ushort*>
struct Foo
{
};

// Specialization -- works when not a specialization
template<
    template<typename,typename> class Container ,
    template<typename , template<typename,typename> class> class MetaFunction
    >
struct Foo<Container<ushort,typename MetaFunction<ushort,Container>::Type> >
{   
    //typedef Container<ushort,typename MetaFunction<ushort,Container>::Type> TestType; // OK
};

int main()
{
}

On compilation (gcc 5.4.0) I get the error:

Test.cpp:14:8: error: template parameters not deducible in partial specialization:
 struct Foo<Container<ushort,typename MetaFunction<ushort,Container>::Type> >
        ^
Test.cpp:14:8: note:         ‘template<class, template<class, class> class<template-parameter-2-2> > class MetaFunction’

Oddly, the argument Container<ushort,typename MetaFunction<ushort,Container>::Type> to the specialization appears to be valid.

like image 778
Olumide Avatar asked Feb 22 '17 12:02

Olumide


1 Answers

The problem here is that

MetaFunction<ushort,Container>::Type

is a non-deduced context, in other words the compiler cannot deduce the template arguments from it, so your specialization is not valid. To see why, read more about it from a previous SO question

What is a nondeduced context?

Basically a non-deduced context boils down to

template<typename T>
struct Identity
{
    using type = T;
};

Now in a pattern like Identity<T>::type, T won't be deduced, although for you it may look obvious (see again the examples in the link I provided for why this is so, it has to do with partial specializations and lack of 1-1 correspondence between types and the members of the specialization).

like image 179
vsoftco Avatar answered Oct 20 '22 15:10

vsoftco