title: C++ user-defined conversion operators without classes?
body: <p>In C++ is it possible to define conversion operators which are not class members? I know how to do that for regular operators (such as +), but not for conversion operators.</p>
  
  <p>Here is my use case: I work with a C Library which hands me out a <code>PA_Unichar *</code>, where the library defines PA_Unichar to be a 16-bit int. It is actually a string coded in UTF-16. I want to convert it to a <code>std::string</code> coded in UTF-8. I have all the conversion code ready and working, and I am only missing the syntactic sugar that would allow me to write:</p>
  
  <pre><code>PA_Unichar *libOutput = theLibraryFunction();
  std::string myString = libOutput;
  </code></pre>
  
  <p>(usually in one line without the temp variable).</p>
  
  <p>Also worth noting:</p>
  
  <ul>
  <li><p>I know that <code>std::string</code> doesn't define implicit conversion from <code>char*</code> and I know why. The same reason might apply here, but that's beside the point.</p></li>
  <li><p>I do have a <code>ustring</code>, subclass of <code>std::string</code> that defines the right conversion operator from <code>PA_Unichar*</code>. It works but this means using <code>ustring</code> variables instead of <code>std::string</code> and that <em>then</em> requires conversion to <code>std::string</code> when I use those strings with other libraries. So that doesn't help very much.</p></li>
  <li><p>Using an assignment operator doesn't work as those <strong>must</strong> be class members.</p></li>
  </ul>
  
  <p>So is it possible to define implicit conversion operators between two types you don't control (in my case <code>PA_Unichar*</code> and <code>std::string</code>), which may or may not be class types?</p>
  
  <p>If not what could be workarounds?</p>