2017-02-22 41 views
3

我会很感激帮助搞清楚一个模板参数是什么这个问题的问世在我的代码,我已经减少了以下事情:错误:类模板部分特例包含无法推断

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>向专业化似乎是有效的。

回答

4

的这里的问题是,

MetaFunction<ushort,Container>::Type 

非推断背景,换句话说,编译器无法从中推断出模板参数,让您的专业是无效的。要知道为什么,阅读更多关于它从以前的SO质疑

What is a nondeduced context?

基本上非推断上下文归结为

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

现在像Identity<T>::type模式,T不会推论,虽然对你来说它可能看起来很明显(再次看到我提供的链接中的例子为什么是这样,它与部分专业化以及类型和专业化成员之间缺乏1-1对应关系)。