错误:类模板部分特化包含无法推导的模板参数

Olu*_*ide 5 c++ templates template-specialization

我很感激帮助弄清楚我的代码中出现的问题是什么,我已经简化为以下内容:

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()
{
}
Run Code Online (Sandbox Code Playgroud)

在编译(gcc 5.4.0)时,我收到错误:

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’
Run Code Online (Sandbox Code Playgroud)

奇怪的是,Container<ushort,typename MetaFunction<ushort,Container>::Type>专业化的论点似乎是有效的.

vso*_*tco 5

这里的问题是

MetaFunction<ushort,Container>::Type
Run Code Online (Sandbox Code Playgroud)

是一个非推导的上下文,换句话说,编译器不能从中推导出模板参数,因此您的专业化无效.要了解原因,请从之前的SO问题中了解更多信息

什么是非弱化背景?

基本上,非演绎的背景可归结为

template<typename T>
struct Identity
{
    using type = T;
};
Run Code Online (Sandbox Code Playgroud)

现在处于一种类似的模式中Identity<T>::type,T不会被推断出来,虽然对你而言可能看起来很明显(再次参见我提供的链接中的例子,为什么会这样,它与部分特化和缺少1-1之间的对应关系类型和专业化的成员).