tags:

views:

1875

answers:

4

I have a loop that reads each line in a file using getline().

istream is;
string line;
while (!getline(is, line).eof())
{
}

I noticed that calling getline() like this also seems to work:

while (getline(is, line))

What's going on here? getline() returns a stream reference. Is it being converted to a pointer somehow? Is this actually a good practice or should I stick to the first form?

+8  A: 

Updated:

I had mistakenly pointed to the basic_istream documentation for the operator bool() method on the basic_istream::sentry class, but as has been pointed out this is not actually what's happening. I've voted up Charles and Luc's correct answers. It's actually operator void*() that's getting called. More on this in the C++ FAQ.

tgamblin
Luc Hermitte
Good point. I also stand corrected.
tgamblin
+11  A: 

The istream returned by getline() is having its operator void*() method implicitly called, which returns whether the stream has run into an error. As such it's making more checks than a call to eof().

Charles Anderson
Charles is right, you're also confusing with the operator provided by the sentry.
Luc Hermitte
Deleted my last posted while I wrote a test. Now I am pretty sure it casts to bool.
Martin York
Unless your SL implementation is non standard, it casts to void*. That's what the standard requires.
Luc Hermitte
I have a copy of the online (read draft) version of the standard. Where are you quoting from? Seem my post below. I will be quite happy to be correct if we can get this nailed down to passage in the standard.
Martin York
C++0x leaves the exact definition of "operator unspecified-bool-type() const;" unspecified (§27.4.4.3/1). C++98 defined "operator void*() const;". If you look, closely, there is no "operator bool()const". You are mistaking with "basic_istream::sentry::operator bool()const"(§27.6.1.1.3/8)
Luc Hermitte
Stand corrected. Good reference.
Martin York
A: 

I would stick with the first form. While the second form may work, it is hardly explicit. Your original code clearly describes what is being done and how it is expected to behave.

e.James
The stream could be bad and not eof(), though. If you want to be explicit, you can call good().
tgamblin
Yes, you are right. It would be best to use the first form, but with a call to .good() instead of .eof()
e.James
Personally I like the use of implicit cast to bool. It looks like all other languages out there that iterate over lines.
Martin York
+4  A: 

Charles did give the correct answer.

What is called is indeed std::basic_ios::operator void*(), and not sentry::operator bool(), which is consistant with the fact that std::getline() returns a std::basic_istream (thus, a std::basic_ios), and not a sentry.

For the non believers, see:

Otherwise, as other have already said, prefer the second form which is canonical. Use not fail() if really you want a verbose code -- I never remember whether xxx.good() can be used instead of !xxx.fail()

Luc Hermitte