Confusing function declaration

F

feminine.aura

I have to read a file containing integers into a vector. I could do
something like this:

ifstream data("file.dat");
istream_iterator<int> begin(data);
istream_iterator<int> end;
vector<int> c(begin,end);

Now this does what i want it to.

But I read in Scott meyer's effective C++ that if I try to get rid of
the second and the thir statement and do something like this

ifstream data("file.dat");
vector<int> c(istream_iterator<int>(data), istream_iterator<int>());

then this wouldn't work as expected because here the second line is
treated as a function declaration where c is the function which returns
a vector<int> and takes two parameters: a)istream_iterator<int> b) a
function pointer which doesnt take any arguments and return a
istream_iterator<int>

And that is the reason why this won't work.

But a friend of mine pointed me to this paper.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1798.html

where it says that such a declaration won't work, not because it is
considered as a function declaration but because an istream_iterator in
this case is considered as a forward iterator and that the
implementation of the constructor for vector which takes a forward
iterator is such that it is not possible to use an istream iterator
with them. I would request you to read the paper and correct me if I
misunderstood what the author said and let me know which one is right.
 
V

Victor Bazarov

I have to read a file containing integers into a vector. I could do
something like this:

ifstream data("file.dat");
istream_iterator<int> begin(data);
istream_iterator<int> end;
vector<int> c(begin,end);

Now this does what i want it to.

But I read in Scott meyer's effective C++ that if I try to get rid of
the second and the thir statement and do something like this

ifstream data("file.dat");
vector<int> c(istream_iterator<int>(data), istream_iterator<int>());

then this wouldn't work as expected because here the second line is
treated as a function declaration where c is the function [...]

Wrap the argument list in a second set of parentheses:

vector<int> c((istream_iterator<int>(data), istream_iterator<int>()));

V
 
F

feminine.aura

Victor,

Thanks for the reply. I know that if I wrap the arguments it would
work. But I was more curious about that paper which had a different
explanation and that is the reason why I posted it here. Two
contradicting statements and I was curious to know which one is right.

Cheers
Priya
 
D

David Harmon

On Wed, 6 Sep 2006 11:03:45 -0400 in comp.lang.c++, "Victor Bazarov"
Wrap the argument list in a second set of parentheses:

vector<int> c((istream_iterator<int>(data), istream_iterator<int>()));

No, wrap each argument

vector<int> c((istream_iterator<int>(data)),
(istream_iterator<int>()));
 
T

Thomas J. Gritzan

Victor said:
Wrap the argument list in a second set of parentheses:

vector<int> c((istream_iterator<int>(data), istream_iterator<int>()));

This shouldn't work.

The comma here is the comma operator, which discards the left hand operand.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top