本文介绍了错误:类模板部分特化包含无法推导出的模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您能帮助我弄清楚我的代码中出现的这个问题是怎么回事,我已将其简化为以下内容:

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

在编译 (gcc 5.4.0) 时出现错误:

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’

奇怪的是,专业化的参数 Container::Type> 似乎是有效的.

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

推荐答案

这里的问题是

MetaFunction<ushort,Container>::Type

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

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

什么是非推导上下文?

基本上一个非推导的上下文归结为

Basically a non-deduced context boils down to

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

现在在像 Identity<T>::type 这样的模式中,T 不会被推导出来,尽管对你来说它可能看起来很明显(再次参见我提供的链接说明为什么会这样,它与部分专业化以及类型和专业化成员之间缺乏 1-1 对应关系有关).

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).

这篇关于错误:类模板部分特化包含无法推导出的模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:40