title: Conversion by cast when indirect route exists
body: <p>Can someone explain the rules of casting, and when a conversion is ambiguous? I'm getting slightly confused by the following case, which <em>gives different answers</em> on MSVC++ (Visual Studio 2010) and gcc-4.3.4.</p>
  
  <pre><code>#include &lt;string&gt;
  
  class myStr
  {
    std::string value;
  
  public:
    myStr(const char* val) : value(val) {}
    operator const char*() const {return value.c_str();}
    operator const std::string() const {return value;}
  };
  
  myStr byVal();
  myStr&amp; byRef();
  const myStr&amp; byConstRef();
  
  int main(int, char**)
  {
    myStr foo("hello");
    std::string test;
  
    // All below conversions fail "ambiguous overload for 'operator='" in gcc
    // Only the indicated coversions fail for MSVC++
    test = foo;  // MSVC++ error "'operator =' is ambiguous"
    test = static_cast&lt;std::string&gt;(foo);
  
    test = byVal();  // MSVC++ error "'operator =' is ambiguous"
    test = static_cast&lt;std::string&gt;(byVal());  // MSVC++ error 
               // "'static_cast' : cannot convert from 'myStr' to 'std::string'"
  
    test = byRef();  // MSVC++ error "'operator =' is ambiguous"
    test = static_cast&lt;std::string&gt;(byRef());
  
    test = byConstRef();  // MSVC++ error "'operator =' is ambiguous"
    test = static_cast&lt;std::string&gt;(byConstRef());
  
    return 0;
  }
  </code></pre>
  
  <p>What rules govern which of those conversions is legal? And is there any compliant way to use unambiguously a class like <code>myStr</code> which defines casts to both <code>const char*</code> and <code>const std::string</code>?</p>