473,387 Members | 1,535 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,387 software developers and data experts.

Compiler oddness with << overload

The oddness is on the last line of code. This is stripped down as far
as I could figure, and I provide several similar examples that work as
expected. I have no idea whether this is a compiler bug or my not
understanding how the << overload is chosen.

-Chris

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

#include <iostream>
#include <sstream>

ostringstream get()
{
return ostringstream();
}

class my_ostringstream : public ostringstream
{
public:
static my_ostringstream get() { return my_ostringstream(); }
~my_ostringstream() { cout << str() << endl; }
};

int main(int argc, char * argv[])
{
ostringstream var1;
var1 << "text 1";
cout << var1.str() << endl; // Output: text 1

ostringstream var2("text 2");
cout << var2.str() << endl; // Output: text 2

ostringstream var3 = get();
var3 << "text 3";
cout << var3.str() << endl; // Output: text 3

{
my_ostringstream var4;
var4 << "text 4";
} // Output: text 4

{
my_ostringstream var5 = my_ostringstream::get();
var5 << "text 5";
} // Output: text 5

{
my_ostringstream::get() << "text 6"; // WTF, seems to be treated
as a void *
} // Output: 00417800
}
Jul 17 '08 #1
1 1486
On Jul 17, 10:05 am, "Alf P. Steinbach" <al...@start.nowrote:
* cedarmillxing...@gmail.com:
Thank you Alf, that helps me more forward a little. It is
still very confusing though.
The void * version
ostream & operator<<(ostream &, const void *)
is an inherited member while the char * version
ostream & operator<<(ostream &, const char *)
is a standalone function (a friend?).
But why does this mean that the void * overload is selected only in
case #6? It seems that either function could have been called.
Quoting myself, "a temporary (rvalue) can't bind to reference
to non-const formal argument".
But you can call a non-const member function on a temporary.
Which is why << void* is found, but << char* not.
This applies to the stream object.
It's probably too late for this round, but I wonder if it
wouldn't be a good idea to require all operator<< to be
non-members. At least the overload set would be consistent.
(IMHO, it would be an even better idea to make operator<< a
template member, with all of the actual instances
specializations. Because it does make sense to do things like
"std::ostringstream() << ...". But of course, doing that would
break considerable code: probably every C++ application in
existance, in fact.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 17 '08 #2

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

Similar topics

1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
5
by: Gianni Mariani | last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the code below: friendop.cpp: In function `int main()': friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,...
25
by: Steve Richter | last post by:
is it possible to overload the << operator in c# similar to the way it is done in c++ ? MyTable table = new MyTable( ) ; MyTableRow row = new MyTableRow( ) ; row << new MyTableCell( "cell1 text...
7
by: glen | last post by:
Hi. I'm using GCC 4.1.1, which I mention since I don't know if this is a compiler issue, or me not understanding some subtlety in the standard. The code below compiles fine under vc++, but I'm...
3
by: johnmmcparland | last post by:
Hi all, I know it is possible to overload the operators and < in C++ but how can I do this. Assume I have a class Date with three int members, m_day, m_month and m_year. In the .cpp files I...
2
by: soy.hohe | last post by:
Hi all I have a class StreamLogger which implements operator << in this way: template <class TStreamLogger& operator<<(const T& t) { <print the stuff using fstream, etc.> return *this; }
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...
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.