Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #10259 > unrolled thread

stl copy and istream_iterator and operator >>

Started by"persres@googlemail.com" <persres@gmail.com>
First post2011-09-18 14:27 -0700
Last post2011-09-19 10:11 +1200
Articles 9 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  stl copy and  istream_iterator and operator >> "persres@googlemail.com" <persres@gmail.com> - 2011-09-18 14:27 -0700
    Re: stl copy and  istream_iterator and operator >> Ian Collins <ian-news@hotmail.com> - 2011-09-19 09:47 +1200
      Re: stl copy and istream_iterator and operator >> "persres@googlemail.com" <persres@gmail.com> - 2011-09-18 14:53 -0700
        Re: stl copy and istream_iterator and operator >> Ian Collins <ian-news@hotmail.com> - 2011-09-19 10:00 +1200
          Re: stl copy and istream_iterator and operator >> "persres@googlemail.com" <persres@gmail.com> - 2011-09-18 15:11 -0700
            Re: stl copy and istream_iterator and operator >> red floyd <no.spam.here@its.invalid> - 2011-09-18 15:23 -0700
            Re: stl copy and istream_iterator and operator >> Ian Collins <ian-news@hotmail.com> - 2011-09-19 10:23 +1200
              Re: stl copy and istream_iterator and operator >> James Kanze <james.kanze@gmail.com> - 2011-09-19 15:19 -0700
      Re: stl copy and  istream_iterator and operator >> Ian Collins <ian-news@hotmail.com> - 2011-09-19 10:11 +1200

#10259 — stl copy and istream_iterator and operator >>

From"persres@googlemail.com" <persres@gmail.com>
Date2011-09-18 14:27 -0700
Subjectstl copy and istream_iterator and operator >>
Message-ID<cd8da4eb-33c9-4b2f-8d75-b2348dcc33a0@s7g2000yqd.googlegroups.com>
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.


[toc] | [next] | [standalone]


#10260

FromIan Collins <ian-news@hotmail.com>
Date2011-09-19 09:47 +1200
Message-ID<9dn78fFr5dU9@mid.individual.net>
In reply to#10259
On 09/19/11 09:27 AM, persres@googlemail.com 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

[toc] | [prev] | [next] | [standalone]


#10261 — Re: stl copy and istream_iterator and operator >>

From"persres@googlemail.com" <persres@gmail.com>
Date2011-09-18 14:53 -0700
SubjectRe: stl copy and istream_iterator and operator >>
Message-ID<513997ec-6237-45a1-8038-7c471c221c87@j1g2000yqn.googlegroups.com>
In reply to#10260
On Sep 18, 10:47 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 09/19/11 09:27 AM, pers...@googlemail.com 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.

[toc] | [prev] | [next] | [standalone]


#10262 — Re: stl copy and istream_iterator and operator >>

FromIan Collins <ian-news@hotmail.com>
Date2011-09-19 10:00 +1200
SubjectRe: stl copy and istream_iterator and operator >>
Message-ID<9dn80fFr5dU10@mid.individual.net>
In reply to#10261
On 09/19/11 09:53 AM, persres@googlemail.com wrote:
> On Sep 18, 10:47 pm, Ian Collins<ian-n...@hotmail.com>  wrote:
>> On 09/19/11 09:27 AM, pers...@googlemail.com 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

[toc] | [prev] | [next] | [standalone]


#10263 — Re: stl copy and istream_iterator and operator >>

From"persres@googlemail.com" <persres@gmail.com>
Date2011-09-18 15:11 -0700
SubjectRe: stl copy and istream_iterator and operator >>
Message-ID<2186df8c-2f6a-41cf-884e-e9c8d98f870f@b10g2000vbz.googlegroups.com>
In reply to#10262
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;

[toc] | [prev] | [next] | [standalone]


#10265 — Re: stl copy and istream_iterator and operator >>

Fromred floyd <no.spam.here@its.invalid>
Date2011-09-18 15:23 -0700
SubjectRe: stl copy and istream_iterator and operator >>
Message-ID<j55r0k$5ll$1@dont-email.me>
In reply to#10263
On 9/18/2011 3:11 PM, persres@googlemail.com 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.

[toc] | [prev] | [next] | [standalone]


#10266 — Re: stl copy and istream_iterator and operator >>

FromIan Collins <ian-news@hotmail.com>
Date2011-09-19 10:23 +1200
SubjectRe: stl copy and istream_iterator and operator >>
Message-ID<9dn9asFr5dU12@mid.individual.net>
In reply to#10263
On 09/19/11 10:11 AM, persres@googlemail.com 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

[toc] | [prev] | [next] | [standalone]


#10303 — Re: stl copy and istream_iterator and operator >>

FromJames Kanze <james.kanze@gmail.com>
Date2011-09-19 15:19 -0700
SubjectRe: stl copy and istream_iterator and operator >>
Message-ID<4b9e693d-efdb-41cb-a479-e05092f4c526@i2g2000yqm.googlegroups.com>
In reply to#10266
On Sep 18, 11:23 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 09/19/11 10:11 AM, pers...@googlemail.com 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

[toc] | [prev] | [next] | [standalone]


#10264

FromIan Collins <ian-news@hotmail.com>
Date2011-09-19 10:11 +1200
Message-ID<9dn8kiFr5dU11@mid.individual.net>
In reply to#10260
On 09/19/11 09:47 AM, Ian Collins wrote:
> On 09/19/11 09:27 AM, persres@googlemail.com 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web