views:

9

answers:

0

Hi all,

I am upgrading a C++ project from MSVC 2008 to 2010, and because of the new CComBSTR move constructor [CComBSTR( CComBSTR&& )], I am getting a compiler error because of an ambiguous call.

Essentially, we have a String class, very similar to std::wstring that have a cast operator to CComBSTR. This is similator to the following code:

class CString {
  public:
    // ...
    operator CComBSTR() {
      CComBSTR temp;
      /* Encoding conversion here */
      return temp;
    }
}

class CObjectConfig {
  public:
    CString GetName() const { return m_name; }

  private:
    CString m_name;
}

Now, at some places in the code, we do the following:

CObjectConfig config = GetObjectConfig();
CComBSTR objectName( config.GetName() );

In VS2008, this would work because the CString object would be implicitly converted to a CComBSTR rvalue and the copy constructor of CComBSTR (taking a const CComBSTR&) would be called to construct objectName.

In VS2010 with C++0x however, the compiler gives an ambiguous call error because CComBSTR rvalue seems to fit both the copy constructor and the move constructor.

While a bit clumsy, my solution to this problem is to static_cast the call to GetName:

CComBSTR objectName( static_cast<const CComBSTR&>( config.GetName() ) );
// or
CComBSTR objectName( static_cast<CComBSTR&&>( config.GetName() ) );

Both lines compile without error, but I need your advice on whether this is illegal, bad practice or undefined. Thank you.