I found something I consider strange behaviour in C++: a typecast operator in a private base class is confusing the compiler when trying to resolve an implicit cast:
#include <iostream>
struct Base
{
#ifdef ENABLE
operator bool () const { return true; }
#endif
};
struct Derived : private Base
{
operator int () const { return 7; }
};
int main()
{
Derived o;
std::cout << o << '\n';
return 0;
}
Without -DENABLE
, the code compiles just fine, and outputs 7
. With -DENABLE
, the code no longer compiles, complaining about an ambiguous overload. I tried gcc-4.6.5
, gcc-4.8.1
, and clang-3.3
. What's confusing is that I clearly cannot ask for (bool)o
, because Base
is a private base.
Is this expected behaviour?