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

c – 与模板功能不匹配

发布时间:2020-12-16 09:31:50 所属栏目:百科 来源:网络整理
导读:在: #include stringvoid f( char const*,char const* = "" ) {}templateclass StringType1,class StringType2 inlinevoid g( StringType1 const s1,StringType2 const s2 = "" ) { f( s1.c_str(),s2.c_str() );}templateclass StringType inlinevoid h( St
在:

#include <string>

void f( char const*,char const* = "" ) {
}

template<class StringType1,class StringType2> inline
void g( StringType1 const &s1,StringType2 const &s2 = "" ) {
  f( s1.c_str(),s2.c_str() );
}

template<class StringType> inline
void h( StringType const &s1,StringType const &s2 = "" ) {
  f( s1.c_str(),s2.c_str() );
}

int main() {             
  std::string s;
  g( s ); // error: no matching function for call to ‘g(std::string&)’
  h( s ); // OK
  return 0;
}

编译器与g()的调用不匹配,因为它有2个模板参数,但它匹配h()就好了.为什么?

仅供参考:我的代码库实际上使用了几个高度专业化的字符串类,所以我想允许最大的灵活性,其中第一个和第二个参数可能是不同的字符串类型.

解决方法

编译器不知道StringType2应该是什么.您需要使用以下内容调用它:

g<std::string,std::string>( s );

让它正常工作.

(编辑:李大同)

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

    推荐文章
      热点阅读