Wednesday, 7 August 2013

conversion operator with template functions

conversion operator with template functions

I have a class with a conversion operator to std::string. It works great
with everything except with functions receiving std::basic_string<T>
(templated on 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 :)
}
The error I receive is
error: no matching function for call to 'G(A&)'
note: candidate is:
note: template<class T> void G(const std::basic_string<_CharT>&)
Now, I know I can define G as a friend in the struct A and it'll work, but
my problem is with a lot of stl functions that already exist and receive
std::basic_string<T> (for example, the operator<< printing function, or
comparison operators, or many other functions.
I would really like to be able to use A as if it was an std::string. Is
there any way to do this?

No comments:

Post a Comment