Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++.moderated > #1424

Re: ambiguous operator on Visual Studio 2010

Message-ID <ispsae$lr6$1@dont-email.me> (permalink)
Newsgroups comp.lang.c++.moderated
From Daniel Krügler <daniel.kruegler@googlemail.com>
Subject Re: ambiguous operator on Visual Studio 2010
Organization A noiseless patient Spider
References <959a4hFnk8U1@mid.individual.net>
Date 2011-06-09 07:03 -0600

Show all headers | View raw


On 2011-06-09 01:23, Aaron Gray wrote:
>
> 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 ?

The user-defined operator>= operator should hide the built-in operator. 
This is described in [over.match.oper] p3 b3

"For the operator ,, the unary operator &, or the operator ->, the 
built-in candidates set is empty. For all other operators, the built-in 
candidates include all of the candidate operator functions defined in 
13.6 that, compared to the given operator,
— have the same operator name, and
— accept the same number of operands, and
— accept operand types to which the given operand or operands can be 
converted according to 13.3.3.1, and
— do not have the same parameter type list as any non-template 
non-member candidate.

In your situation the last sub-bullet applies, that is, your operator>= 
overload *does* have the same parameter type list as the built-in 
operator, therefore the built-in operator is not a candidate.

[over.built] p1 emphasis this with the note:

"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."

Therefore the compiler you are using has a defect.

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

Try to encourage the compiler vendor to fix the current state.

HTH & Greetings from Bremen,

- Daniel Krügler


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

Back to comp.lang.c++.moderated | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

ambiguous operator on Visual Studio 2010  "Aaron Gray" <ang.usenet@gmail.com> - 2011-06-08 17:23 -0600
  Re: ambiguous operator on Visual Studio 2010  Gnawme <norm.evangelista@gmail.com> - 2011-06-09 07:00 -0600
  Re: ambiguous operator on Visual Studio 2010  Daniel Krügler <daniel.kruegler@googlemail.com> - 2011-06-09 07:03 -0600

csiph-web