Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6800525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:58:47+00:00 2026-05-26T18:58:47+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:58:48+00:00Added an answer on May 26, 2026 at 6:58 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of any existing packages or libraries that can be used to
Does anyone know how I can, in platform-independent C++ code prevent an object from
Does anyone know where online copies of the old The Perl Journal articles can
Does anyone know how can I do a string check inside a string? for
Does anyone know of, or can recommend, a library that can recursively visualize an
Does anyone know what the expression is to take the YEAR only from the
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone know how to transform a enum value to a human readable value?
Does anyone know of a good Command Prompt replacement? I've tried bash/Cygwin, but that
Does anyone know of a FOSS Python lib for generating Identicons ? I've looked,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.