2017-02-22 68 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對應關係)。