469,951 Members | 2,681 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

A question about istringstream

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! :)
Jul 22 '05 #1
6 2191
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:41***************@ucalgary.ca...
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

Jul 22 '05 #2
Ali Çehreli wrote:
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:41***************@ucalgary.ca...
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?


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

Ali


Jul 22 '05 #3
> 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
Jul 22 '05 #4
Jonathan Mcdougall wrote:
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.


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...

Jonathan


Jul 22 '05 #5
JustSomeGuy wrote:
Jonathan Mcdougall wrote:

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.

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
Jul 22 '05 #6
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:41***************@ucalgary.ca...
Ali Çehreli wrote:
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:41***************@ucalgary.ca...
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?


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

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Samuele Armondi | last post: by
8 posts views Thread by Agent Mulder | last post: by
3 posts views Thread by bml | last post: by
7 posts views Thread by Luther Baker | last post: by
4 posts views Thread by Brandon | last post: by
8 posts views Thread by Randy Yates | last post: by
6 posts views Thread by James Aguilar | last post: by
11 posts views Thread by icanoop | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.