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

  • Home
  • SEARCH
  • 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 8151183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:17:41+00:00 2026-06-06T15:17:41+00:00

Bjarne suggests using the condition in if’s as scope restriction. In particular this example.

  • 0

Bjarne suggests using the condition in if’s as scope restriction. In particular this example.

if ( double d = fd()  ) {
   // d in scope here...
}

I’m curios how to interpret the declaration in a true / false sense.

  1. It’s a declaration
  2. It’s a double.

Edit:
It’s in 6.3.2.1 The C++ programming language as a recommendation.

Edit2: templatetypedefs suggestion of pointers, in particular with dynamic casts, might give insight to Bjarnes suggestion.

SteveJessop tells me: – A condition is not an expression it can also be a declaration, the value used, is the value being evaluated.

  • 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-06T15:17:43+00:00Added an answer on June 6, 2026 at 3:17 pm

    The code that you’re seeing is a specialized technique for declaring variables in if statements. You commonly see something like this:

    if (T* ptr = function()) {
        /* ptr is non-NULL, do something with it here */
    } else {
        /* ptr is NULL, and moreover is out of scope and can't be used here. */
    }
    

    A particularly common case is the use of dynamic_cast here:

    if (Derived* dPtr = dynamic_cast<Derived*>(basePtr)) {
         /* basePtr really points at a Derived, so use dPtr as a pointer to it. */
    } else {
         /* basePtr doesn't point at a Derived, but we can't use dPtr here anyway. */
    }
    

    What’s happening in your case is that you’re declaring a double inside the if statement. C++ automatically interprets any nonzero value as true and any zero value as false. What this code means is “declare d and set it equal to fd(). If it is nonzero, then execute the if statement.”

    That said, this is a Very Bad Idea because doubles are subject to all sorts of rounding errors that prevent them from being 0 in most cases. This code will almost certainly execute the body of the if statement unless function is very well-behaved.

    Hope this helps!

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

Sidebar

Related Questions

I ran into this supposed interview of Bjarne Stroustrup, the inventor of C++. http://artlung.com/smorgasborg/Invention_of_Cplusplus.shtml
I'm not sure what Bjarne meant with this exercise: Convert the desk calculator to
Is a type specifier required here? const c = 7; Bjarne Stroustrup's 'The C++
I am reading Bjarne Stroustrup's Programming : Principles and Practice Using C++ In the
This is taken right from The C++ Programming Language by Bjarne Stroustrup. I would
I am trying an example from Bjarne Stroustrup's C++ book, third edition. While implementing
In N3257 I found an example using initializing members without a constructor , which
This excerpt is from Bjarne's book : In principle, an exception is copied when
How is this statement a definition? Isn't it supposed to be a declaration only
I'm studying The C++ Programming Language from Bjarne Stroustrup and he talks about logical

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.