Implicit cast to object that implements overloaded operator

P

pnsteiner

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
}
 
G

Greg

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
}

Instead of having test implement an ostream conversion operator, why
not simply write a global << operator for an ostream (on the left) and
a test (on the right)?

Greg
 
V

Victor Bazarov

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.

Only if the operator is declared a member. The compiler is not required
to apply all possible conversions to try to find which conversion would
be necessary to _then_ resolve a member function call.
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?

There is no way to "hint".
> (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
}

Use what works.

V
 
K

Karl Heinz Buchegger

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.

An operator is nothing else then an ordinary function with some strange syntax.
So when your compiler sees.
test t;

t << "test\n"; // implicit cast doesn't compile

it transforms this into:

t.operator<<( "test\n" );

Then the compiler scans the class test, searching for a function operator<<.
Finding none, the compiler searches the freestanding functions for a standalone

.... operator<<( test&, const char* );

Finding none, the compiler gives up and emits an error message.
The problem with that is that the compiler doesn't try to cast an
object to something else, in order to be able to come up with a function
it can call. Either that object's class (or one of its base classes) has
that function or it has not.
 
P

pnsteiner

thanks for the prompt answer. it's clear to me now why implicit cast
doesn't make any sense here, thinking of operators being nothing more
than fancy method invokation syntax...
 
P

pnsteiner

because the given code is just a sample for my problem. i have a class
with lots of global << operator definitions all over the place, and i
would like to be able to use those on another (unrelated) class without
redefining all those operators...
 
V

Victor Bazarov

thanks for the prompt answer. it's clear to me now why implicit cast
doesn't make any sense here, thinking of operators being nothing more
than fancy method invokation syntax...

Actually, if operator<< with 'ostream&' as LHS and 'const char*' as RHS
*were* a non-member, the conversion would be considered. Take a look:

struct A {
void operator + (const char*);
};

void operator - (A&, const char*);

// operator+ is a member, operator- is not

struct B {
operator A& ();
};

int main() {
B b;
b + "abc"; // error here
b - "abc"; // _no_ error here
}

V
 
P

pnsteiner

i see, very interesting. actually my problem was about the usage of
non-member operator functions, i was just unknowingly using the member
operator << defined in ostream& in my test program, not recognizing the
difference between member and non-member operator function definitions.

thanks alot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top