Can anyone explain what is happening here? I am trying to create overloaded relational operators for class member enums. VC6.0 and gcc 3.3.4 compile the code fine. VC7.0 and 7.1 complain that the overloaded operator function is ambiguous. The following code illustrates the problem:
VC6.0 and gcc compile cleanly. VC7.0 and 7.1 fail with the following error:
Compiling...
enumtest.cpp
d:\misc\test.cpp(54) : error C2593: 'operator >' is ambiguous
d:\misc\test.cpp(13): could be 'bool operator >(A::level,A::level)'
or 'built-in C++ operator>(A::level, A::level)'
while trying to match the argument list '(A::level, A::level)'
What are the built-in operators mentioned? Is the compiler assuming that an enum-is-an-enum-is-an enum, and thus bypasses the strong typing? What is the correct way to write this that conforms to the standard and is portable?
Any and all insights will be welcome.
Code:
class A
{
public:
enum level
{
none, low, medium, high
};
};
bool operator>( A::level l1, A::level l2 )
{
bool greater = false;
switch( l1 )
{
case A::low:
switch( l2 )
{
case A::none: greater = true; break;
}
break;
case A::medium:
switch( l2 )
{
case A::low:
case A::none: greater = true; break;
}
break;
case A::high:
switch( l2 )
{
case A::medium:
case A::low:
case A::none: greater = true; break;
}
break;
}
return greater;
}
int
main( int argc, char * argv[] )
{
A::level l1 = A::high;
A::level l2 = A::low;
if( l1 < l2 )
{
return 1;
}
if( l1 > l2 )
{
return -1;
}
return 0;
}
VC6.0 and gcc compile cleanly. VC7.0 and 7.1 fail with the following error:
Compiling...
enumtest.cpp
d:\misc\test.cpp(54) : error C2593: 'operator >' is ambiguous
d:\misc\test.cpp(13): could be 'bool operator >(A::level,A::level)'
or 'built-in C++ operator>(A::level, A::level)'
while trying to match the argument list '(A::level, A::level)'
What are the built-in operators mentioned? Is the compiler assuming that an enum-is-an-enum-is-an enum, and thus bypasses the strong typing? What is the correct way to write this that conforms to the standard and is portable?
Any and all insights will be welcome.