In C ++, can I define conversion operators that are not members of a class? I know how to do this for regular operators (e.g. +), but not for conversion operators.
Here is my use case: I am working with a C library that tells me PA_Unichar *where the library defines PA_Unichar as a 16-bit int. This is actually a string encoded in UTF-16. I want to convert it std::stringto UTF-8 encoded. I have conversion code ready and working and I just skip the syntactic sugar that would let me write:
PA_Unichar *libOutput = theLibraryFunction();
std::string myString = libOutput;
(usually in one line without temp variable).
Also worth noting:
I know that it std::stringdoes not define an implicit conversion from char*, and I know why. The same reason can be applied here, but this is not relevant.
I have a ustringsubclass std::stringthat defines the correct conversion operator from PA_Unichar*. It works, but that means using variables ustringinstead std::string, and then requires conversion to std::stringwhen I use these lines with other libraries. So it doesnβt help much.
Using an assignment operator does not work, since they must be members of the class.
Thus, you can define implicit conversion operators between two types that you do not control (in my case PA_Unichar*and std::string), which may or may not be class types?
?