25

Does anyone know why istream object can be used as bool expression? For example:

ifstream input("tmp");
int iValue;
while (input >> iValue)
    //do something;

Here input >> iValue returns a reference to the ifstream object. I want to know why this object can be used as a bool expression.
I look into the ifstream class and find that this may be due to the following member function:

operator void * ( ) const;

See here for detail about this function.
If it is, can anyone explain this function to me? The prototype of this function is different from usual operator overload declaration. What is the return type of this function?
If it is not, then what is the reason that ifstream object can be used as bool expression?
Looking forward to your help!

cheng

Saurav Sahu
  • 9,755
  • 5
  • 42
  • 64
cheng
  • 1,936
  • 5
  • 25
  • 34

2 Answers2

24

The exact mechanism that enables use of an istream as a boolean expression, was changed in C++11. Previously is was an implicit conversion to void*, as you've found. In C++11 it is instead an explicit conversion to bool.

Use of an istream or ostream in a boolean expression was enabled so that C++ programmers could continue to use an expression with side-effects as the condition of a while or for loop:

SomeType v;

while( stream >> v )
{
    // ...
}

And the reason that programmers do that and want that, is that it gives more concise code, code that is easier to take in at a glance, than e.g. …

for( ;; )
{
    SomeType v;
    
    stream >> v;
    if( stream.fail() )
    {
        break;
    }
    // ...
}

However, in some cases even such a verbose structure can be preferable. It depends.

halfer
  • 18,701
  • 13
  • 79
  • 158
Cheers and hth. - Alf
  • 135,616
  • 15
  • 192
  • 304
7

It's a cast operator to the given type. operator T () is a cast operator to the type T. In the if statement, the ifstream is converted to void*, and then the void* converted to bool.

Puppy
  • 138,897
  • 33
  • 232
  • 446
  • 4
    `void* converted to bool` .. that's not correct. Rather I would say, `void* is compared to 0`. – iammilind Nov 14 '11 at 05:21
  • Thanks for replying so soon, I got it. I have one more question: if there are two cast operator functions in a class, one to int and the other to int*. For the call if (obj), which case operation function will be called? Why? Thanks. – cheng Nov 14 '11 at 05:21
  • 4
    @iammilind: It is correct. The pointer is converted to `bool`. Check §4.12 and §6.4.1. – R. Martinho Fernandes Nov 14 '11 at 05:30
  • @R.MartinhoFernandes, Are you referring to C++11. AFAIK, in C++03, it's just plain `void*` which is compared with `0`. [P.S.: I haven't downvoted this answer.] – iammilind Nov 14 '11 at 06:13
  • @iammilind: The reference numbers I gave are from the C++11 standard. The numbering in C++03 could be different. *But the behaviour is the same*. `void*` is convertible to `bool` and everything is converted to `bool` in `if`s. – R. Martinho Fernandes Nov 14 '11 at 06:14
  • 3
    Comparison to 0 is how the conversion is implemented, but it is entirely correct to say that it is converted. – Puppy Nov 14 '11 at 08:45