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 9206251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:08:10+00:00 2026-06-18T00:08:10+00:00

When I run code like istream s; if (s) // … I can test

  • 0

When I run code like

istream s;
if (s)
  // ...

I can test for the truth value of istream (or any ios object). From my understanding, this works the way it does because of how ios defines the operator! function. (For reference, http://www.cplusplus.com/reference/ios/ios/operatornot/.) My question is: why does putting s inside the if statement, as above, cause the operator! function to be evaluated? As a secondary question, if I put just any object inside an if statement, what are the possible ways that C++ will use to evaluate its truthiness?

  • 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-06-18T00:08:11+00:00Added an answer on June 18, 2026 at 12:08 am

    In C++03, this check was done through the operator void* member function, which returned a non-null pointer if the stream was in a good state and NULL otherwise. Since null pointers evaluate to false and non-null pointers evaluate to true, this check worked fine.

    It did have problems, though, since you could do this:

    std::cout << std::cout;
    

    This would invoke the operator void* function and print out either 0 or the address of cout, depending on whether the stream was good. This isn’t a good thing, since the above code doesn’t appear to do that. Worse, you could do

    delete std::cout;
    

    Which causes undefined behavior if the stream isn’t good.

    In C++11, this was changed so the stream has an explicit operator bool member function, which allows the stream to be explicitly cast to a bool. This eliminates the above problem and is the preferred way to support if checks on objects going forward.

    Hope this helps!

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

Sidebar

Related Questions

Taking the jQuery framework for example, if you run code like this: $(document).ready(function init()
Based on this question: How can I get HTML source code from TWebBrowser If
I've run into another issue while writing this code (with MUCH help from programmers
I like to run some code when a user closes my ms 2000 app.
I would like to run some code onload of a form in WPF. Is
I'd like some Javascript code to run when the mouse leaves the browser window.
When i run the below code I am getting error like ex = {The
I'd like to run a spell checker on the docstrings of my Python code,
I have some code that I'd like to run on a page. My problem
I have some Objective-C [i-os] code that I would like to run unit tests

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.