Boost logo

Boost :

From: Jonathan Wakely (cow_at_[hidden])
Date: 2005-02-17 04:48:18


On Thu, Feb 17, 2005 at 12:48:35AM -0500, Jason Hise wrote:

> Sometimes I like to play around with obscure code, just to see what it
> does. The following really surprised me:
>
>
> #include <iostream>
>
> struct A
> {
> public:
> bool operator == ( const A & rhs )
> {
> std::cout << "A\n";
> return true;
> }
> };
>
> struct B
> {
> public:
> bool operator == ( const B & rhs )
> {
> std::cout << "B\n";
> return false;
> }
> };
>
> struct C : public A, public B { };
>
> int main ( )
> {
> C c1, c2;
> if ( c1 == c2 ) std::cout << "Equal\n";
> return 0;
> }
>
> Compiled on MSVC 7.1, this outputs:
> A
> Equal
>
> I would have expected this code not to compile at all, because the ==
> overload seems ambiguous. Is this result standards conforming? If not,
> what would the standards conforming result be? Does this fall into the
> realm of undefined behavior?

No, you're first expectation was right - it's ambiguous, so it's an error.

Comeau's online compiler rejects it, as does every version of GCC I can
lay my hands on. Seems like a VC++ bug.

GCC says:
ambig.cc: In function 'int main()':
ambig.cc:28: error: request for member 'operator==' is ambiguous
ambig.cc:16: error: candidates are: bool B::operator==(const B&)
ambig.cc:6: error: bool A::operator==(const A&)

Comeau says:
"ComeauTest.c", line 28: error: more than one operator "==" matches these operands:
            "C::operator==" (ambiguous by inheritance)
            operand types are: C == C
     if ( c1 == c2 ) std::cout << "Equal\n";

jon

-- 
"For a successful technology, reality must take precedence over public
 relations, for Nature cannot be fooled."
	- Richard P. Feynman

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk