我有一个带有转换运算符的类std::string。它适用于所有功能,除了接收函数std::basic_string<T>(模板上T)。
#include <string>
struct A{
operator std::string(){return std::string();}
};
void F(const std::basic_string<char> &){}
template<typename T> void G(const std::basic_string<T> &) {}
int main(){
A a;
F(a); // Works!
G(a); // Error!
return 0; // because otherwise I'll get a lot of comments :)
}
我收到的错误是
error: no matching function for call to 'G(A&)'
note: candidate is:
note: template<class T> void G(const std::basic_string<_CharT>&)
现在,我知道我可以G在结构中定义为朋友,A它会工作,但我的问题是很多已经存在和接收的 stl 函数std::basic_string<T>(例如,operator<<打印函数,或比较运算符,或许多其他函数.
我真的很希望能够像A使用std::string. 有没有办法做到这一点?