J
jacek.dziedzic
Hi!
I have a function that, depending on a boolean condition, should
read data from either an ifstream or a stringstream. Passing the
respective stream to the function by reference or pointer is out of
the question. I already have the function for the ifstream version, so
instead of writing everything twice, I wanted to take a shortcut:
// ifs is the ifstream, ss is the stringstream
istream input (use_stringstream ? ss : ifs);
input >> ...
getline(input,s); ...
This, however, doesn't work, because streams cannot be copied. So I
tried with
istream& input (use_stringstream ? ss : ifs);
but I need an lvalue when I want to assign to a reference...
A const-reference is no good, because I work on the stream later on,
changing its state.
Is there any way I could do it?
TIA,
- J.
I have a function that, depending on a boolean condition, should
read data from either an ifstream or a stringstream. Passing the
respective stream to the function by reference or pointer is out of
the question. I already have the function for the ifstream version, so
instead of writing everything twice, I wanted to take a shortcut:
// ifs is the ifstream, ss is the stringstream
istream input (use_stringstream ? ss : ifs);
input >> ...
getline(input,s); ...
This, however, doesn't work, because streams cannot be copied. So I
tried with
istream& input (use_stringstream ? ss : ifs);
but I need an lvalue when I want to assign to a reference...
A const-reference is no good, because I work on the stream later on,
changing its state.
Is there any way I could do it?
TIA,
- J.