Why can't I construct an std::istream_iterator with an unnamed temporary?

Posted by Stumped6789 on Stack Overflow See other posts from Stack Overflow or by Stumped6789
Published on 2010-05-06T16:52:04Z Indexed on 2010/05/06 16:58 UTC
Read the original article Hit count: 116

Filed under:
|
|

g++ allows this construction of an istream_iterator from an ifstream instance:

std::ifstream ifstr("test.txt");
std::istream_iterator<std::string> iter1(ifstr);

...but it doesn't allow the same construction with an unnamed temporary:

std::istream_iterator<std::string> iter2(std::ifstream("test.txt"));

This gives:

error: no matching function for call to ‘std::istream_iterator, ptrdiff_t>::istream_iterator(std::ifstream)’

Does anyone know why this doesn't work? - thanks!

© Stack Overflow or respective owner

Related posts about c++

Related posts about istream-iterator