我很感激帮助弄清楚我的代码中出现的这个问题是怎么回事,我已将其简化为以下内容:
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()
{
}
在编译 (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’
奇怪的是,参数 Container<ushort,typename MetaFunction<ushort,Container>::Type>
特化似乎是有效的。
最佳答案
这里的问题是
MetaFunction<ushort,Container>::Type
是一个非推导上下文,换句话说,编译器无法从中推导模板参数,因此您的特化无效。要了解原因,请从之前的 SO 问题中阅读更多相关信息
基本上,非推导上下文归结为
template<typename T>
struct Identity
{
using type = T;
};
现在的模式类似于 Identity<T>::type
, T
不会推导出来,尽管对您来说它可能看起来很明显(请再次查看我提供的链接中的示例,了解为什么会这样,它与部分特化以及类型和成员之间缺乏 1-1 对应关系有关特化)。
关于c++ - 错误 : class template partial specialization contains a template parameter that cannot be deduced,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42391745/