Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++.moderated > #1414 > unrolled thread
| Started by | "Aaron Gray" <ang.usenet@gmail.com> |
|---|---|
| First post | 2011-06-08 17:23 -0600 |
| Last post | 2011-06-09 07:03 -0600 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.c++.moderated
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
| From | "Aaron Gray" <ang.usenet@gmail.com> |
|---|---|
| Date | 2011-06-08 17:23 -0600 |
| Subject | ambiguous operator on Visual Studio 2010 |
| Message-ID | <959a4hFnk8U1@mid.individual.net> |
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
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
[toc] | [next] | [standalone]
| From | Gnawme <norm.evangelista@gmail.com> |
|---|---|
| Date | 2011-06-09 07:00 -0600 |
| Message-ID | <f3cc2ea4-8961-4550-8895-0af1cd5cde71@m21g2000yqc.googlegroups.com> |
| In reply to | #1414 |
{ quoted moderation server banner removed. -mod }
On Jun 8, 4:23 pm, "Aaron Gray" <ang.use...@gmail.com> 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 ?
>
> Is there a workaround other than defining a function 'bool
superset(flags,flags)' instead of operator >= ?
>
> Many thanks in advance,
>
> Aaron
>
This is a known defect in Visual Studio:
http://support.microsoft.com/kb/128344
The KB article contains their suggestion for a workaround.
Alternatively, using your overloaded operator in a function call
syntax works:
bool b = operator>=(f1, f2);
I glanced at the C++ spec, but didn't see anything governing 'correct'
behavior in this case.
Norm
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
[toc] | [prev] | [next] | [standalone]
| From | Daniel Krügler <daniel.kruegler@googlemail.com> |
|---|---|
| Date | 2011-06-09 07:03 -0600 |
| Message-ID | <ispsae$lr6$1@dont-email.me> |
| In reply to | #1414 |
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! ]
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++.moderated
csiph-web