加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:11:00 所属栏目:百科 来源:网络整理
导读:myTemplateTemplate期望第二个模板参数是一个带有一个参数的模板. myDefaultTemplate是一个带有两个参数的模板,第二个参数的默认值为int. 在VS2008中,我得到编译错误:类模板’myDefaultTemplate’的模板参数列表与模板参数’TT’的模板参数列表不匹配 那么
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参数模板.默认参数不会被考虑.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读