473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Incorrect operator<< is called.

Can someone explain what's going on with the test program below ?

On Windows / Visual Studio .NET

os << x << endl; uses operator<< ( ostream & , const string & )

but

cout << x << endl; uses operator<< ( ostream & , const X & ) as I expect.

On Linux / g++, operator<< ( ostream & , const X & ) is used in both cases.

Is this a bug on Windows or is there something in the standard that I'm not
aware off ?

Test program:

#include <iostream>
#include <istream>
#include <string>
#include <sstream>

using namespace std;

class X
: public string
{ };

ostream& operator<< ( ostream& strm, const X& x )
{
strm << "Okidoki : '" << static_cast<string>( x ) << "'";
return strm;
}

int main( void )
{
X x;
ostringstream os;

os << x << endl;

cout << os.str( );

return 0;
}

Thanks.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
2 1367
After replying to this a minute ago and selecting "reply to all groups", I
remembered something about how crossposted messages are held up from being
posted to the non-moderated groups until the moderated ones accept it.
Since it can take a while for stuff to show up on clcm, I'll risk some ire
and post this again /just here/ so that folks might benefit from the
research I put into this. Sorry in advance for when (if?) it shows up
again...

-----------------

I've spent a while trying to pin this one down, and all I've managed to
accomplish so far is to get frustrated. First, note that the problem only
happens under VC6 and VC7.0 (under VC7.1 it works fine, as it does under
every other platform I have handy here to test on.) I've found several ways
to work around the problem, the simplest being to just cast the stream in
your insertion statement thusly:

(ostream &) os << x << endl;

Another thing you can do (in order to avoid adding the ugly casts
everywhere) would be to define another inserter that takes an ostringstream
rather than an ostream:

ostream& operator<< ( ostringstream& strm, const X& x )
{
strm << "Okidoki : '" << static_cast<string>( x ) << "'";
return strm;
}

All this leads me to suspect that either some template support deficiency
in the VC6/7.0 compiler is the culprit, or else it is something in the
libraries they use. However, the fact I get exactly the same (wrong)
behavior when using the Dinkum drop-in replacement libs with VC6 and VC7.0
makes me lean toward suggesting it is the compilers that are to blame.

HTH,
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2

I think there is a MS specific solution of the operator for the class
ostringstream in VC++ 6.

Ralf

www.oo-factory.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
2
by: Terry Santegoeds | last post by:
Can someone explain what's going on with the test program below ? On Windows / Visual Studio .NET os << x << endl; uses operator<< ( ostream & , const string & ) but cout << x << endl;...
3
by: Alicia | last post by:
Hello, I am trying to figure out how to call an overloaded operator<< inherited from a base class. #ifndef PHONECALL #define PHONECALL #include "time.h" #include "interval.h"
0
by: ma740988 | last post by:
Consider #include <iostream> #include <string> #include <map> using namespace std; struct dstream // data_stream class {
3
by: Carlo Capelli | last post by:
I found a change in the following code, that behaved correctly in VC++ 6. #include <strstream> using namespace std; void main() { char x; ostrstream(x, 100) << "pippo" << "pluto" << ends;...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
1
by: buburuz | last post by:
Hi, I have a question about overloading operator<< . Actually I am trying to understand how it works when chaining multiple calls to this operator. I have a very simple class (MyOut) with an...
4
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right?...
3
by: subramanian100in | last post by:
Consider the code: #include <iostream> using namespace std; int main( ) { cout << "test string "; cout.operator<<(10).operator<<(endl);
3
by: Martin T. | last post by:
Hello. I tried to overload the operator<< for implicit printing of wchar_t string on a char stream. Normally using it on a ostream will succeed as std::operator<<<std::char_traits<char> will...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.