为什么带有默认模板参数的模板不能用作模板模板参数中具有较少模板参数的模板 [英] Why the template with default template arguments can't be used as template with less template argument in Template Template Parameters

查看:97
本文介绍了为什么带有默认模板参数的模板不能用作模板模板参数中具有较少模板参数的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

myTemplateTemplate期望第二个模板参数是具有一个参数的模板。
myDefaultTemplate是一个具有两个参数的模板,第二个参数具有默认类型int。

myTemplateTemplate expects the second template parameter is a template with one argument. myDefaultTemplate is a template with two arguments, and the second argument has default type int.

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

In VS2008, I get the compile error:the template parameter list for class template 'myDefaultTemplate' does not match the template parameter list for template parameter 'TT'

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

So,why the myDefaultTemplate can't be used as the template with just one argument? Are there any negative impact if C++ compiler supports it?

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;
}


推荐答案

.3第1段 - [temp.arg.template):

From the standard (see 14.3.3 paragraph 1 - [temp.arg.template):


模板模板参数的模板参数应为
类模板的名称,表示为id-expression。当将模板模板
参数与相应的参数匹配时,仅考虑主
类模板;即使它们的参数列表与
模板模板参数的参数列表匹配,也不考虑部分专业化。

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个参数模板。不会考虑默认参数。

That means the template myDefaultTemplate will be seen only as 2 arguments template. The default argument will not be considered.

这篇关于为什么带有默认模板参数的模板不能用作模板模板参数中具有较少模板参数的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆