c++ - conversion operator with template functions -


i have class conversion operator std::string. works great except 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;   f(a); // works!   g(a); // error!   return 0; // because otherwise i'll lot of comments :) } 

the error receive

error: no matching function call 'g(a&)'                                      note: candidate is: note: template<class t> void g(const std::basic_string<_chart>&) 

now, know can define g friend in struct a , it'll work, problem lot of stl functions exist , receive std::basic_string<t> (for example, operator<< printing function, or comparison operators, or many other functions.

i able use a if std::string. there way this?

i able use if std::string. there way this?

yes, sure want this? solution is:

struct : public std::string { }; 

but recall std::string doesn't have virtual destructor , therefore, cannot used polymorphically. you have been warned!!!

a str() far better solution , allows explicit when want pass a function taking std::basic_string<t>.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -