微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 为什么默认模板参数的模板不能用作模板模板参数中具有较少模板参数的模板

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

解决方法

从标准(见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参数模板.认参数不会被考虑.

原文地址:https://www.jb51.cc/c/112385.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐