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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:36:08+00:00 2026-05-28T14:36:08+00:00

Perhaps I am getting rusty (have been writing in Python recently). Why does this

  • 0

Perhaps I am getting rusty (have been writing in Python recently).

Why does this not compile?

if ( (int i=f()) == 0)

without the () around the int i=f() I get another, much more reasonable error of i is not being boolean. But that’s why I wanted the parentheses in the first place!

My guess would be that using the parentheses makes it into an expression, and that declaration statements are not allowed in an expression. Is it so? And if yes, is it one of the C++’s syntax quirks?

BTW, I was actually trying to do this:

if ( (Mymap::iterator it = m.find(name)) != m.end())
    return it->second;
  • 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-28T14:36:09+00:00Added an answer on May 28, 2026 at 2:36 pm

    You can declare a variable in the if statement in C++ but it is restricted to be used with direct initialization and it needs to convert to a Boolean value:

    if (int i = f()) { ... }
    

    C++ doesn’t have anything which could be described as “declaration expression”, i.e. [sub-] expressions declaring a variable.

    Actually, I just looked up the clause in the standard and both forms of initialization are supported according to 6.4 [stmt.select] paragraph 1:

    ...
    condition:
       expression
       attribute-specifier-seqopt decl-specifier-seq declarator = initializer-clause
       attribute-specifier-seqopt decl-specifier-seq declarator braced-init-list
    ...
    

    That is, it is also be possible to write:

    if (int i{f()}) { ... }
    

    Obviously, this only works in C++2011 because C++2003 doesn’t have brace-initialization.

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

Sidebar

Related Questions

Perhaps I am not asking or searching for this correctly: I want to have
Perhaps I am not getting SQL Parameters correctly, but this doesn't work for me.
I have this following code and I am not getting the results I expected.
I'm perhaps being a bit lazy asking this here, but I'm just getting started
A quick question of perhaps a more speculative nature. I've been getting heavy into
I've been getting some strange errors ( perhaps once per day ) after updating
I've always just used SELECT COUNT(1) FROM X but perhaps this is not the
I have been unsuccessful getting a Java WebStart application that should have AllPermissions to
I'm getting a headache from this query, I have no idea how to solve
Perhaps this is the wrong place for this, but I'm not quite sure where

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.