Microsoft KB Archive/128344

= FIX: Error C2593: 'operator +' Is Ambiguous =

Article ID: 128344

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft Visual C++ 4.2 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 4.0 Professional Edition
 * Microsoft Visual C++ 2.1
 * Microsoft Visual C++ 1.52 Professional Edition
 * Microsoft Visual C++ 5.0 Learning Edition
 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 5.0 Learning Edition
 * Microsoft Visual C++ 1.51
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft C/C++ Professional Development System 7.0

-



This article was previously published under Q128344



SYMPTOMS
When enumerated types are used in some (+-*/|&^%) operator overloads, the Microsoft compilers listed above may generate the following message:

Error C2593: 'operator +' is ambiguous

In Visual C++, 32-bit edition, version 2.1, the following warnings are also displayed:

Warning C4387: 'enum Test __cdecl operator+(enum Test, enum Test)': was considered

Warning C4388: and built-in global operator was considered

The "Sample Code" section in this article demonstrates this problem.



CAUSE
Without determining that there is an exact operator overload match, the compiler is considering the enumerator types promoted to int type. This causes the C2593 ambiguous error.



RESOLUTION
Use one of the following three workarounds:  With Visual C++ 32-bit Edition, versions 2.x and 4.x, you can work around the problem by using a class template. See the "Sample Code to Work Around Problem" section in this article. This workaround provides a class template that can be used for any enum type. One of the parameters for the overloaded operator needs to be of the class template type and the enums need to be declared as the class type. -or-

  You can call the overloaded operator using normal function call syntax. For example: eTest = operator+( eTest1, eTest2 );

-or-

  You can just use the built-in global operator, and then convert the result back to enum type. For example: eTest = Test( (int)eTest1 + eTest2 ); where eTest, eTest1 and eTest2 are of type enum Test{}. 



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++ version 5.0.



Sample Code to Reproduce Problem
/* Compiler options needed: None. */

enum Test { ONE, TWO, THREE };

Test operator+(Test eTest, Test eTest2) {     Test   eRet = Test(int(eTest) + int(eTest2)); return eRet; }

void main {     Test    eTest=ONE, eTest2=TWO; eTest = eTest + eTest2;    // C2593 error }

Sample Code to Work Around Problem
/* Compiler options needed: None. */

enum Test { ONE=1, TWO, THREE };

template class CFix {     T m_fix;

public:

CFix(T eFix) { m_fix = eFix; }

operator T { return m_fix; } };

Test operator+(CFix eTest, Test eTest2) {     Test   eRet = Test(int(eTest) + int(eTest2)); return eRet; }

void main {     CFix<Test>  eTest=ONE, eTest2=TWO; eTest = eTest + eTest2; }

<div class="references_section">