2011-07-08 121 views
10

myTemplateTemplate期望第二个模板参数是具有一个参数的模板。 myDefaultTemplate是一个包含两个参数的模板,第二个参数的默认类型为int。为什么具有默认模板参数的模板无法在模板模板参数中用作模板参数较少的模板

在VS2008中,我得到的编译错误:类模板的模板参数列表“myDefaultTemplate”不匹配模板参数“TT”

那么,为什么不能使用myDefaultTemplate模板参数列表作为只有一个参数的模板? 如果C++编译器支持它,会有什么负面影响吗?

template 
<typename T1, typename T2 = int> 
class 
myDefaultTemplate{ 
     T1 a; 
     T2 b; 
}; 

template 
<typename T1, template<typename T2> class TT> 
class 
myTemplateTemplate{ 
     T1 a; 
     TT<T1> b; 
}; 

int main(int argc, char* argv[]){ 
     myTemplateTemplate<int, myDefaultTemplate> bar; //error here:  
     return 0; 
} 
+15

您应该获得“最常使用'模板'这个词的问题”的徽章:) :) – Praetorian

回答

7

从标准(见14.3.3段1 - [temp.arg.template):

A template-argument for a template template-parameter shall be the name of a class template, expressed as id-expression. Only primary class templates are considered when matching the template template argument with the corresponding parameter; partial specializations are not considered even if their parameter lists match that of the template template parameter.

这意味着该模板myDefaultTemplate可以看出仅作为2个参数模板。默认参数将不被考虑。

+0

是的,这是我期望的答案。谢谢 – RolandXu