std::getline() returns
Asked Answered
O

4

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?

Oxpecker answered 3/11, 2008 at 16:56 Comment(0)
H
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().

Hexagram answered 3/11, 2008 at 17:2 Comment(5)
Charles is right, you're also confusing with the operator provided by the sentry.Wit
Deleted my last posted while I wrote a test. Now I am pretty sure it casts to bool.Anam
Unless your SL implementation is non standard, it casts to void*. That's what the standard requires.Wit
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.Anam
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)Wit
G
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.

Godding answered 3/11, 2008 at 17:2 Comment(1)
You are confusing the sentry with basic_ios::operator void*() -> dinkumware.com/manuals/…*Wit
W
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()

Wit answered 3/11, 2008 at 18:1 Comment(1)
Indeed. dinkumware has removed its online documentation. Link fixed. Thanks.Wit
B
-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.

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

© 2022 - 2024 — McMap. All rights reserved.