25

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?

4 Answers 4

27

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

Sign up to request clarification or add additional context in comments.

5 Comments

Charles is right, you're also confusing with the operator provided by the sentry.
Deleted my last posted while I wrote a test. Now I am pretty sure it casts to bool.
Unless your SL implementation is non standard, it casts to void*. That's what the standard requires.
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.
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)
8

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.

1 Comment

You are confusing the sentry with basic_ios::operator void*() -> dinkumware.com/manuals/…*
5

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()

1 Comment

Indeed. dinkumware has removed its online documentation. Link fixed. Thanks.
-3

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.

3 Comments

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.