From what I've read, getline()
used in a Boolean context returns an implicitly conversion to void*
. I haven't found anywhere on the web any real reference to this statement. Everywhere it says that implicit conversion doesn't exist and that in a Boolean context pointers should be of the same kind (and if ptr == 0
than 0
is converted to type of the pointer ptr
).
Also in the standard says in a Boolean context it is converted to an unspecified-Boolean-type. What does that even mean?
In short:
It means that you can use
getline()
in aif
statement and if it works you enter theif
statement block.In Long:
The above is not technically correct (but that is the result).
getline()
actually returns a reference to the stream it was used on. When the stream is used in a Boolean context this is converted into a unspecified type (C++03) that can be used in a Boolean context. In C++11, this was updated and it is converted tobool
.getline()
succeeded it returns a stream in a good state. When this is converted to abool
like type it returns a non-null pointer (C++03) which when used in a Boolean context is equivalent totrue
.getline()
fails it returns a stream in a bad state. When this is converted to abool
like type it returns a null pointer (C++03) which when used in a Boolean context is equivalent tofalse
.A null
void*
in a Boolean context is equivalent tofalse
, any othervoid*
is equivalent totrue
. (though the type is actually unspecified but you can think of it as avoid*
(just to make it easy to think about).It means you can use it any conditional statements:
It doesn't say it explicitly anywhere on the web because it involves putting three different facts together.
getline()
returnsistream&
istream
is convertable tovoid*
istream
is used in boolean context (such as anif
orwhile
statement).