c++ - 错误 : class template partial specialization contains a template parameter that cannot be deduced

标签 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()
{
}

在编译 (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 问题中阅读更多相关信息

What is a nondeduced context?

基本上,非推导上下文归结为

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/

相关文章:

c++ - 纯抽象类与其实现之间的数据隔离与纯抽象类的真正含义?

c++ - 需要一个轻量级的 C++ 模板引擎

c++ - 使用 Microchip XC16 编译器将 foo.cpp 构建为 C 代码

c++ - boost::is_member_function_pointer_of?

c++ - 阻止模板类的所有实例化——包括支持的类型

c# - 两种可能的编程模式,哪一种更合适?

c++ - 为什么 C++ 实例化一个被完全特化掩盖的基本模板函数?

c++ - 如何在 Ubuntu 中按照 C++11 标准编译?

c++ - 如何防止函数模板中的隐式转换?

c++ - 强制非专用模板实例化出现编译错误