473,509 Members | 2,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String streams and operator << misbehaving

Hi everyone,

Following advice previously given in this group, I've created a function
that I'm using to "inline" the creation of a string. Regardless of what
you think of my method, I'm confused as to why the operator <<
overloading seems to be selecting the wrong function.

If you look at the example program below, it should print this:

The string is 'Five == 5'

However the first variable in the createString() parameter is always
treated as an integer or pointer, instead of its native type - hence you
end up with something like this:

The string is '0x804b485 == 5'

Could someone please enlighten me as to what's causing this behaviour?
Even if I pass "std::ios::dec" as the first parameter, this results in a
"2" being printed, which I find very unusual.

Thanks,
Adam.
#include <sstream>
#include <stdio.h>

inline std::string _createString(const std::basic_ostream<char,
std::char_traits<char &sstr)
throw ()
{
return static_cast<const std::ostringstream&>(sstr).str();
}

#define createString(a) _createString(std::ostringstream() << a)

int main(void)
{
std::string strTest = createString("Five" << " == " << 5);

printf("The string is '%s'\n", strTest.c_str());
return 0;
}
Nov 30 '07 #1
2 1453
Adam Nielsen wrote:
Hi everyone,
[Snipped description of the problem: OP is asking why the wrong operator<< is
called on the string stream.]
>
#include <sstream>
#include <stdio.h>

inline std::string _createString(const std::basic_ostream<char,
std::char_traits<char &sstr)
throw ()
{
return static_cast<const std::ostringstream&>(sstr).str();
}

#define createString(a) _createString(std::ostringstream() << a)

int main(void)
{
std::string strTest = createString("Five" << " == " << 5);

printf("The string is '%s'\n", strTest.c_str());
return 0;
}
This question comes up once in three months (maybe we should put it in the FAQ?)
The strange behaviour you are experiencing is due to the fact that the ostream
object you are putting your code into is a temporary object. For some reasons,
C++ only lets you call _member functions_ on temporary objects.

Since std::ostream provides only a limited set of methods for printing very
basic types (the overloads for your own types are non-member functions), it will
choose std::ostream::operator<<(const void*).

#include <sstream>
#include <memory>
#include <stdio.h>

std::auto_ptr<std::ostringstreamGetStream ()
{
return std::auto_ptr<std::ostringstream(new std::ostringstream);
}

#define createString(a) \
(static_cast<const std::ostringstream&>(((*GetStream ().get()) << a)).str ())

int main(void)
{
std::string strTest = createString("Five" << " == " << 5);

printf("The string is '%s'\n", strTest.c_str());
return 0;
}

Regards,
Stuart
Nov 30 '07 #2
The strange behaviour you are experiencing is due to the fact that the
ostream object you are putting your code into is a temporary object. For
some reasons, C++ only lets you call _member functions_ on temporary
objects.

Since std::ostream provides only a limited set of methods for printing
very basic types (the overloads for your own types are non-member
functions), it will choose std::ostream::operator<<(const void*).
Ah, that makes sense. Thanks for the explanation! I'm sure there's a
logical reason why that behaviour is intentional. At any rate your
solution works quite nicely!

Cheers,
Adam.
Dec 4 '07 #3

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

Similar topics

3
8245
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
2
2504
by: gbgbgbgb | last post by:
Hi, I have a definition bool operator<(string s_s, string s_t) { .... } and a variable list<string> concomp;
3
1887
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"
4
2222
by: Matthias | last post by:
Now I am confused. I am doing a "less" comparison in my program between strings, to see which strings in some range precede others. This is my approach: class SortedByName { public: bool...
3
2027
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
3
2932
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
14
2312
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> ...
4
2047
by: bluekite2000 | last post by:
Here A is an instantiation of class Matrix. This means whenever user writes Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2 //and fills it up w/ random value cout<<A; the...
24
17436
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET...
0
7137
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
7347
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
7416
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...
0
5656
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
4732
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3218
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3207
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.