A question about istringstream

J

JustSomeGuy

I am passing an istringstream to a function. I want that function to
get a copy of the istringstream and not a refrence to it.
ie when the function returns I want the istringstream to be
unmodified...

However when I try to pass it

fn(istringstream s) // doesn't compile
but
fn(istringstream & s) // does.

What should I do to?

I tried to make a copy:

istringstream hold = s
but that doesn't compile either...

help! :)
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

JustSomeGuy said:
I am passing an istringstream to a function. I want that function to
get a copy of the istringstream and not a refrence to it.

istringstream (and possibly any stream) cannot be copied.
ie when the function returns I want the istringstream to be
unmodified...

However when I try to pass it

fn(istringstream s) // doesn't compile
but
fn(istringstream & s) // does.

How about passing it by const reference:

void fn(istringstream const & s);

But then, you can't do much with a const input stream anyway. What are
trying to achieve?

Ali
 
J

JustSomeGuy

Ali said:
istringstream (and possibly any stream) cannot be copied.


How about passing it by const reference:

void fn(istringstream const & s);

But then, you can't do much with a const input stream anyway. What are
trying to achieve?

Just tying to extract data from the stream and leave it the same state.
 
J

Jonathan Mcdougall

I am passing an istringstream to a function. I want that function to
get a copy of the istringstream and not a refrence to it.

streams can't be copied.
ie when the function returns I want the istringstream to be
unmodified...

make it a const reference.
However when I try to pass it

fn(istringstream s) // doesn't compile
but
fn(istringstream & s) // does.

What should I do to?

fn(const istringstream &s)

If you really need to work on a copy, do it manually (but the parameter
will always have to be a const reference because you can't pass streams
by value).

void fn(const std::istringstream &iss);

int main()
{
std::istringstream iss;

// fill 'iss'

std::istringstream copy_of_iss;
copy_of_iss.str(iss.str());

fn(copy_of_iss);
}

So fn() works on a reference to a copy.

Jonathan
 
J

JustSomeGuy

Jonathan said:
streams can't be copied.


make it a const reference.


fn(const istringstream &s)

If you really need to work on a copy, do it manually (but the parameter
will always have to be a const reference because you can't pass streams
by value).

void fn(const std::istringstream &iss);

int main()
{
std::istringstream iss;

// fill 'iss'

std::istringstream copy_of_iss;
copy_of_iss.str(iss.str());

fn(copy_of_iss);
}

So fn() works on a reference to a copy.

This sounds like a good idea.. but when you make a copy of the iss stream
does the current 'stream buffer point' get preserved..
ie if you've read (red) 3 characters from the input stream will you get
the fourth character on the next read?
I guess the bottom line is that the operator= hasn't been defined for this
stream class...
 
J

Jonathan Mcdougall

JustSomeGuy said:
Jonathan Mcdougall wrote:




This sounds like a good idea.. but when you make a copy of the iss stream

I don't, I defined a new object using the same string as its buffer.
does the current 'stream buffer point' get preserved..

Of course not, nor its state or whatever data it has, nothing got
copied. I just made a new istringstream and set its buffer to the same
string as the first one.

Particularily, to set the buffer's read position, just do something like

copy_of_iss.seekg(iss.tellg());
I guess the bottom line is that the operator= hasn't been defined for this
stream class...

The bottom line is: streams are not copyable by design.


Jonathan
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

JustSomeGuy said:
Just tying to extract data from the stream and leave it the same state.

Unfortunately this is not guaranteed to work for input streams. Think about
the standard input: Characters taken out of it are gone forever unless we
save them.

The best you can do is to save all of the characters into a stream. Then you
can use seekg to set the read position before returning from your function.

The following program worked for me. Send two words to the standard input of
the program to see that consecutive calls to foo maintain the read position
of the stream.

#include <sstream>
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

class PositionSaver
{
istream & is_;
ios::pos_type position_;

public:

explicit PositionSaver(istream & is)
:
is_(is),
position_(is_.tellg())
{}

~PositionSaver()
{
is_.seekg(position_);
}
};

void foo(istream & input)
{
PositionSaver const position_saver(input);

string first_word;
input >> first_word;
cout << "first word read in foo : " << first_word << '\n';
}

int main()
{
stringstream my_stream;
copy(istream_iterator<string>(cin),
istream_iterator<string>(),
ostream_iterator<string>(my_stream, " "));

string first_word;
my_stream >> first_word;
cout << "first word read in main: "
<< first_word << '\n';

foo(my_stream);
foo(my_stream);
}

Ali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top