在:
#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;
}
Run Code Online (Sandbox Code Playgroud)
编译器与调用不匹配,g()因为它有2个模板参数,但它匹配h()得很好.为什么?
仅供参考:我的代码库实际上使用了几个高度专业化的字符串类,因此我希望允许最大的灵活性,其中第一个和第二个参数可能是不同的字符串类型.
编译器不知道StringType2应该是什么.您需要使用以下内容调用它:
g<std::string, std::string>( s );
Run Code Online (Sandbox Code Playgroud)
让它正常工作.
| 归档时间: |
|
| 查看次数: |
151 次 |
| 最近记录: |