stl copy and istream_iterator and operator >>

P

persres

Hi,
I have the following code -

string str("12 3213 asdf 21");
vector<int> v;

copy(istream_iterator<int>(istringstream(str)) ,
istream_iterator<int>(), back_inserter(v) );

I expected copy to throw because the operator >> would have failed due
to the 'asdf' string present.
However, it doesn't throw. Do you know why,
How can i get it to throw or return an errror.
 
I

Ian Collins

Hi,
I have the following code -

string str("12 3213 asdf 21");
vector<int> v;

copy(istream_iterator<int>(istringstream(str)) ,
istream_iterator<int>(), back_inserter(v) );

This shouldn't compile. You can't create an istream_iterator from a
temporary.
I expected copy to throw because the operator>> would have failed due
to the 'asdf' string present.

Which operator >>?
However, it doesn't throw. Do you know why,

Because the code doesn't do what you think it does (assuming you get it
to compile). Have a look at the vector contents.
How can i get it to throw or return an errror.

You can provide your own input iterator type.
 
P

persres

This shouldn't compile. You can't create an istream_iterator from a
temporary.


Which operator >>?


Because the code doesn't do what you think it does (assuming you get it
to compile).  Have a look at the vector contents.


You can provide your own input iterator type.

It does compile and works as expected. This is standard way of
copying.
 
I

Ian Collins

Please don't quote signatures
It does compile and works as expected. This is standard way of
copying.

I'd better try it then...

cat x.cc
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>

using namespace std;

int main() {
string str("12 3213 asdf 21");
vector<int> v;

copy(istream_iterator<int>(istringstream(str)) ,
istream_iterator<int>(), back_inserter(v) );
}

g++ x.cc

x.cc: In function ‘int main()’:
x.cc:13:48: error: no matching function for call to
‘std::istream_iterator<int>::istream_iterator(std::istringstream)’

CC x.cc
"x.cc", line 13: Error: Cannot cast from std::istringstream to
std::istream_iterator<int, char, std::char_traits<char>, int>.
 
P

persres

http://www.sgi.com/tech/stl/istream_iterator.html

I am using a temporary. I suppose the life time of the temporary will
be valid until copy is done.

I am in VS 2008.

there is a constructore like this -

istream_iterator(istream_type& _Istr)
: _Myistr(&_Istr)
{ // construct with input stream
_Getval();
}

where
typedef basic_istream<_Elem, _Traits> istream_type;
 
I

Ian Collins

This shouldn't compile. You can't create an istream_iterator from a
temporary.


Which operator>>?

I wasn't being very helpful there (Monday morning).
std::istream_iterator will stop after the first extraction failure, so
if you want more error checking you will have to roll your own.
 
R

red floyd

http://www.sgi.com/tech/stl/istream_iterator.html

I am using a temporary. I suppose the life time of the temporary will
be valid until copy is done.

I am in VS 2008.

there is a constructore like this -

istream_iterator(istream_type& _Istr)
: _Myistr(&_Istr)
{ // construct with input stream
_Getval();
}

where
typedef basic_istream<_Elem, _Traits> istream_type;

Turn on /Za. What you're seeing is a MS extension.
You can't pass a temporary to a function expecting a
non-const reference.

MS allows it as an extension.
 
I

Ian Collins

http://www.sgi.com/tech/stl/istream_iterator.html

I am using a temporary. I suppose the life time of the temporary will
be valid until copy is done.

I am in VS 2008.

there is a constructore like this -

istream_iterator(istream_type& _Istr)
: _Myistr(&_Istr)
{ // construct with input stream
_Getval();
}

where
typedef basic_istream<_Elem, _Traits> istream_type;

I temporary should have the type "const istream_type", so the matching
function signature would be istream_iterator(const istream_type& _Istr).
 
J

James Kanze

I temporary should have the type "const istream_type", so the matching
function signature would be istream_iterator(const istream_type& _Istr).

The temporary has the type std::istringstream. But C++ doesn't
allow initializing a reference to a non-const with an rvalue.
(Hasn't, in fact, since something like 1988.) The type (and the
const-ness of the type) doesn't enter into it, at least for the
initializer.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top