Boost logo

Boost :

From: Jason Hise (chaos_at_[hidden])
Date: 2005-02-17 00:48:35


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?

-Jason


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