BUG: No Compiler Error for Ambiguous C++ Conversion (116487)



The information in this article applies to:

  • Microsoft Visual C++ for Windows, 16-bit edition 1.0
  • Microsoft Visual C++ for Windows, 16-bit edition 1.5
  • Microsoft Visual C++, 32-bit Editions 1.0
  • Microsoft Visual C++, 32-bit Editions 2.0
  • Microsoft Visual C++, 32-bit Editions 4.0
  • Microsoft Visual C++, 32-bit Editions 4.1
  • Microsoft Visual C++, 32-bit Editions 4.2
  • Microsoft Visual C++, 32-bit Editions 5.0
  • Microsoft Visual C++, 32-bit Editions 6.0
  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)

This article was previously published under Q116487

SYMPTOMS

Class A has a member function that converts an instance of class B to an instance of class A. Class B also has a member function that converts an instance of class B to an instance of class A. Therefore, when you assign an instance of class B to an instance of class A, the compiler could use both conversion methods, resulting in an ambiguity. However, the C/C++ compiler does not generate an error message in this situation, as demonstrated by the sample code in the "MORE INFORMATION" section, below.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

You can use the following sample code to reproduce this problem:

Sample Code

/* Compile options needed: none
*/ 

#include <iostream.h>

class B;

class A
{
public:
    // This is the function the compiler
    // chooses to call.
     A(B&) { cout << "called A::A(B&)" << endl; }
};

class B
{
public:
     operator A()  // This function does not get called.
     {
          B b;
          cout << "called operator B::A()" << endl;
          return b;
     }
};

void main()
{
        B b;
        A a = b; // A(b) or b.operator A()?
                 // The compiler should issue an
                 // error here but instead chooses
                 // to call A::A(B&).

        cout << "failed: should not compile- see ARM 12.3.2" << endl;
}

				

REFERENCES

"The Annotated C++ Reference Manual" (ARM), Ellis and Stroustrup, Section 12.3.2, "Conversion Functions."

Modification Type:MinorLast Reviewed:7/5/2005
Keywords:kbBug kbCompiler kbCPPonly KB116487