i want to use the << operator defined for ostream with an object that
itself knows nothing of the operator, but is castable to ostream&. it
seems that C++ doesn't do implicit casts before invoking a custom
operator.
i've tested with visual c++ 7 and the comeau online compilers, both
required an explicit cast. is there a way to hint the compiler at using
the cast operator implicitely, or is this not part of the language at
all? (i have a scenario where this would come in very handy, though i
am not sure if i would like c++ to behave like this anyways :)
#include <iostream>
using namespace std;
class test
{
public:
operator ostream& ()
{
return cout;
}
};
void test()
{
test t;
t << "test\n"; // implicit cast doesn't compile
((ostream&)t) << "test\n"; // explicit cast works
}