Re: How to call operator<< functions?

Michael Veksler
2006-08-31T12:05:56Z

Daniel Jacobowitz wrote:
> On Wed, Aug 30, 2006 at 11:45:33PM +0300, Michael Veksler wrote:
>> Great progress. The most annoying and common failures I have used to
>> be seeing is no more.
>
> I will test and commit the patch (but not right this moment).
>
No hurry, I have managed for eight years, I can cope with it a bit more.
>> The other annoying issues sorted by decreasing annoyance :
>> - this->Print is not always found (I'll try to create a small
>> test-case later).
>
> I think my other changes will help here, though I'm not sure.
I doubt they will. I have produced a complete test case for that. See
below.
>> - std::cout related crashes
>
> Fred's pegged this one I suspect. This is a serious bug and we need to
> fix it, but it will be a bit tricky.
My following test case may be related to this.
>> - print myCout << x --- won't work need to resort to
>> print 'operator<<........'(myCout, x)
>
> I think that foo::operator<< is supported, but operator<<(foo&,...)
> isn't. Or else something's wrong with the support.
Well, it works without the std:: as the prefix of operator<<.
I hoped that just using << (as in C++) would just work.
==========
Here is the test case.
=> cat cout-gdb2.cpp
#include <iostream>

using namespace std;
extern int forceLink;

struct A
{
virtual void Print(ostream &out) const = 0;
};

struct B : public A
{
virtual void Print(ostream &out) const ;
};

void B::Print(ostream &out) const
{
out << "Enter B::Print() this=" << this << endl;
}
ostream & operator<<(ostream & out, const A& data)
{
data.Print(out);
}
int main()
{
B x;
const A & ref_x = x;
ref_x.Print(cout);

return forceLink;
}
=> cat myCout.cpp
#include <iostream>
using namespace std;
ostream myCout(cout.rdbuf());
// A trick to force the linker put this object file into the executable
int forceLink=0;

=> g++ -g cout-gdb.cpp myCout.cpp
=> gdb-6.5 -silent a.out
Using host libthread_db library "/lib/tls/libthread_db.so.1".
(gdb) b 30
Breakpoint 1 at 0x8048a62: file cout-gdb.cpp, line 30.
(gdb) r
Starting program: /home/veksler/a.out
Enter B::Print() this=0xbfffec00

Breakpoint 1, main () at cout-gdb.cpp:30
30 return forceLink;
(gdb) p myCout
$1 = <incomplete type>
(gdb) p x.Print(myCout)
Cannot resolve method B::Print to any overloaded instance
(gdb) p x.Print
$2 = &B::Print(std::ostream&) const
(gdb) p $2(&x, myCout)
Enter B::Print() this=0xbfffec00
$3 = void
(gdb)