Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stl copy and istream_iterator and operator >>

Reply
Thread Tools

stl copy and istream_iterator and operator >>

 
 
persres@googlemail.com
Guest
Posts: n/a
 
      09-18-2011
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.



 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-18-2011
On 09/19/11 09:27 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.

--
Ian Collins
 
Reply With Quote
 
 
 
 
persres@googlemail.com
Guest
Posts: n/a
 
      09-18-2011
On Sep 18, 10:47*pm, Ian Collins <(E-Mail Removed)> wrote:
> On 09/19/11 09:27 AM, (E-Mail Removed) wrote:
>
> > 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.
>
> --
> Ian Collins


It does compile and works as expected. This is standard way of
copying.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-18-2011
On 09/19/11 09:53 AM, (E-Mail Removed) wrote:
> On Sep 18, 10:47 pm, Ian Collins<(E-Mail Removed)> wrote:
>> On 09/19/11 09:27 AM, (E-Mail Removed) wrote:
>>
>>> 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.


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

--
Ian Collins
 
Reply With Quote
 
persres@googlemail.com
Guest
Posts: n/a
 
      09-18-2011
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;

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-18-2011
On 09/19/11 09:47 AM, Ian Collins wrote:
> On 09/19/11 09:27 AM, (E-Mail Removed) wrote:
>> 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>>?


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.

--
Ian Collins
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      09-18-2011
On 9/18/2011 3:11 PM, (E-Mail Removed) wrote:
> 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.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-18-2011
On 09/19/11 10:11 AM, (E-Mail Removed) wrote:
> 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).

--
Ian Collins
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-19-2011
On Sep 18, 11:23 pm, Ian Collins <(E-Mail Removed)> wrote:
> On 09/19/11 10:11 AM, (E-Mail Removed) wrote:
> >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).


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.

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
Problem | Istream_iterator in std::copy Pradeep C++ 5 10-19-2006 04:51 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
copy and istream_iterator question George C++ 6 12-19-2005 07:10 PM
istream_iterator and ostream_iterator problem Chris Mantoulidis C++ 2 12-15-2003 12:14 PM



Advertisments