Kyle said:
Steven T. Hatton wrote:
I'm curious, what was the reason given for this design feature?
Reason is an interesting word in this context. I will say there was an
argument presented. There may actually be a _reason_, but I am unaware of
it. The argument is that the std::stringstream defined as it is follows
the same semantics as std::fstream. I tried to find an example of why I
would want this behavior, but I didn't find any satisfying examples. The
best I can come up with on my own is that I might use std::stringstream as
the backing of an edit buffer. With the current design, the insert point
would be set at the beginning of the "file" if it were initialized by
passing a string.
Is
there a constructor that allows you to set the write pointer to the
"end" of the used portion of the internal buffer? (Sorry, all of my
references are at work)
I'll admit I have really only used the
stringstreams as either input or output exclusively. Just for
clarification:
ostringstream ostr("My name is ");
ostr << "Kyle.";
cout << ostr.str();
What does the standard say should be the result of this output? If I
understand you correctly, are you saying it should be "Kyle.me is "
according to the standard? I would expect "My name is Kyle.".
I'm only a page ahead of you on this. I've only read about it, and used it
for a few toy programs sofar. (and one little micro app). Try the
following as-is. and then remove the 'o' in std:
stringstream, and see
what happens.
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
std::string str1() {
std::stringstream s;
s <<""
"Whoever limiting his worldly ambitions finds satisfaction in the
speculative\n"
"life has in the approval of an enlightened and competent judge a
powerful\n"
"incentive to labours, the benefits of which are great but remote,
and\n"
"therefore such as the vulgar altogether fail to recognise.\n"
"To such a judge and to his gracious attention I now dedicate this work,
and\n"
"to his"
<< std::endl;
return s.str();
}
std::string str2() {
std::stringstream s;
s <<"What is mind? No Matter. What is matter? Never mind."<< std::endl;
return s.str();
}
int main() {
std::stringstream s(str1());
s << str2();
std::cout << s.str() << std::endl;
std:
stringstream ss(str1(), std::ios_base::app);
ss << str2();
std::cerr << std::boolalpha << ss.fail() << std::endl;
std::cout << ss.str() << std::endl;
}
This is the output I get from the version shown:
What is mind? No Matter. What is matter? Never mind.
tion in the speculative
life has in the approval of an enlightened and competent judge a powerful
incentive to labours, the benefits of which are great but remote, and
therefore such as the vulgar altogether fail to recognise.
To such a judge and to his gracious attention I now dedicate this work, and
to his
false
Whoever limiting his worldly ambitions finds satisfaction in the speculative
life has in the approval of an enlightened and competent judge a powerful
incentive to labours, the benefits of which are great but remote, and
therefore such as the vulgar altogether fail to recognise.
To such a judge and to his gracious attention I now dedicate this work, and
to his
What is mind? No Matter. What is matter? Never mind.
Why? If you wanted to be extra careful checking for errors, it seems
like a two step process:
1.) Attempt an operation on the stream.
2.) Query the stream for a bad state.
I don't see how the str("") would be the operation to put the stream
into a bad state? Why check it at this point? I'm not sure about this,
but would the str("") even succeed if the stream was already in a bad
state, i.e. would the internal buffer be deallocated and set to an empty
string?
I hadn't expressed myself well. I mean that ther may be situation where
that would be the desierable thing. For example, if the user didn't know
exactly what was passed to str(). That operation might take place at a
point where checking the error is not convenent. IOW, the operation it it
clear()ed would be implicitly destroying state information whether that's
what the user wants or not. str(""); is just another call to set the value
of the string. It's only special because it happens to put the string into
a commonly desired state. It might just as easily happen arbitrarily. For
example if the string passed represented an empty line of text.
That is exactly what I was thinking. So if anyone has an example,
please share. Thanks!
As far as the call to clear(), in the previous thread where I originally
posted this question, someone basically said that it was not enough to
just call str(""), but that it had to be accompanied with a call to
clear(). That is what got me thinking about why str("") would not
guarantee that the stream is in a good state....
Thanks for your reply,
Kyle
Run the code I posted with, and without the 'o', and see if it doesn't get
you back to wondering.