A strange C++ compiler behavior caught me by surprise today.
class Foo {
friend ostream& operator << (ostream& os, const Foo& rhs)
{
return os << sizeof(rhs) << endl;
}
};
struct Bar { };
int main()
{
Foo f(Bar());
cout << f;
return 0;
}
Apparantly, the code above compiled fine, even though there was no
Foo(const Bar&);
c'tor defined. Compiling with -Wall showed that the compiler interprets the expression
Foo f(Bar());
as a prototype for a function
Foo f(Bar (*)())
instead of an expression for creating an object of type Foo.
What really is the catch (rule) here??