title: Implicit conversion to std::string
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/6677072/overload-resolution-failure-when-streaming-object-via-implicit-conversion-to-str">Overload resolution failure when streaming object via implicit conversion to string</a>  </p>
  </blockquote>
  
  
  
  <p>I know it's not such a good idea to do this, but I really want to know the reason why the code below does not compile (i.e. why there is "no acceptable conversion"):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  
  class Test
  {
  public:
      operator std::string () const;
  };
  
  Test::operator std::string () const
  {
      return std::string("Test!");
  }
  
  int main ()
  {
      std::string str = "Blah!";
      std::cout &lt;&lt; str &lt;&lt; std::endl;
  
      Test test;
  
      str = test;//implicitly calls operator std::string without complaining
  
      std::cout &lt;&lt; str &lt;&lt; std::endl;
  
      std::cout &lt;&lt; test;//refuses to implicitly cast test to std::string
  
      return 0;
  }
  </code></pre>
  
  <p>On Visual Studio 2010 I get this error: "<code>error C2679: binary '&lt;&lt;' : no operator found which takes a right-hand operand of type 'Test' (or there is no acceptable conversion)</code>"</p>
  
  <p>Does the <code>&lt;&lt;</code> operator implicitly cast <code>std::string</code> to something else in order to make use of it? If yes, what operator do I need to overload in my class to make such a thing work? I refuse to believe that I would actually need to use <code>operator char *</code>.</p>