Discussion:
ambiguous operator on Visual Studio 2010
(too old to reply)
Aaron Gray
2011-06-08 13:15:39 UTC
Permalink
Hello,

I am getting an ambiguous operator error in VS2010 but not in GCC 4.5.

The following code illustrates the problem :-


enum flags { OK, TEST };

inline bool operator >= (flags f1, flags f2) {
return (f1 & f2) == f2;
}

int main(int argc, char *argv[]) {
flags f1 = OK;
flags f2 = TEST;
bool b = f1 >= f2;
return 0;
}


gives the following on VS2010 :-

1> test.cpp
1>c:\test\c++\operator-test\operator-test\test.cpp(10): error C2593:
'operator >=' is ambiguous
1> c:\test\c++\operator-test\operator-test\test.cpp(3): could be 'bool
operator >=(flags,flags)'
1> or 'built-in C++ operator>=(flags, flags)'
1> while trying to match the argument list '(flags, flags)'

What is the correct ISO C++ 2004 behaviour ?

Is there a workaround other than defining a function 'bool
superset(flags,flags)' instead of operator >= ?

Many thanks in advance,

Aaron
M. Shoaib Surya
2011-06-08 15:46:35 UTC
Permalink
Yes, it's a bug in VC, confirmed by Microsoft:
http://connect.microsoft.com/VisualStudio/feedback/details/529700/enum-operator-overloading-broken

- Shoaib.
Post by Aaron Gray
Hello,
I am getting an ambiguous operator error in VS2010 but not in GCC 4.5.
The following code illustrates the problem :-
enum flags { OK, TEST };
inline bool operator >= (flags f1, flags f2) {
return (f1 & f2) == f2;
}
int main(int argc, char *argv[]) {
flags f1 = OK;
flags f2 = TEST;
bool b = f1 >= f2;
return 0;
}
gives the following on VS2010 :-
1> test.cpp
'operator >=' is ambiguous
1> c:\test\c++\operator-test\operator-test\test.cpp(3): could be 'bool
operator >=(flags,flags)'
1> or 'built-in C++ operator>=(flags, flags)'
1> while trying to match the argument list '(flags, flags)'
What is the correct ISO C++ 2004 behaviour ?
Is there a workaround other than defining a function 'bool
superset(flags,flags)' instead of operator >= ?
Many thanks in advance,
Aaron
Cholo Lennon
2011-06-08 16:24:43 UTC
Permalink
Post by Aaron Gray
Hello,
I am getting an ambiguous operator error in VS2010 but not in GCC 4.5.
The following code illustrates the problem :-
enum flags { OK, TEST };
inline bool operator >= (flags f1, flags f2) {
return (f1 & f2) == f2;
}
int main(int argc, char *argv[]) {
flags f1 = OK;
flags f2 = TEST;
bool b = f1 >= f2;
return 0;
}
gives the following on VS2010 :-
1> test.cpp
'operator >=' is ambiguous
1> c:\test\c++\operator-test\operator-test\test.cpp(3): could be 'bool
operator >=(flags,flags)'
1> or 'built-in C++ operator>=(flags, flags)'
1> while trying to match the argument list '(flags, flags)'
AFAIK is VC++ bug (VC++ 2005 has the same behavior, but not Comeau
online compiler)
Post by Aaron Gray
What is the correct ISO C++ 2004 behaviour ?
ISO C++ 98 (and 2003 and 2010) said (13.6 Buil-in operators):

"If there is a user-written candidate with the same name and parameter
types as a built-in candidate operator function, the built-in operator
function is hidden and is not included in the set of candidate functions"
Post by Aaron Gray
Is there a workaround other than defining a function 'bool
superset(flags,flags)' instead of operator >= ?
You can manually call the operator: bool b = operator >= (f1, f2);
Post by Aaron Gray
Many thanks in advance,
Aaron
Regards
--
Cholo Lennon
Bs.As.
ARG
Loading...