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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:34:28+00:00 2026-05-26T09:34:28+00:00

This is a follow-up to C++, variable declaration in ‘if’ expression if( int x

  • 0

This is a follow-up to C++, variable declaration in ‘if’ expression

if( int x = 3 && true && 12 > 11 )
    x = 1;

The rules (so far as I can tell) are:

  1. can only have 1 variable declared per expression
  2. variable declaration must occur first in the expression
  3. must use copy initialization syntax not direct initialization syntax
  4. cannot have parenthesis around declaration

1 and 2 make sense according to this answer but I can’t see any reason for 3 and 4. Can anyone else?

  • 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-26T09:34:28+00:00Added an answer on May 26, 2026 at 9:34 am

    C++03 standard defines selection statement as:

    selection-statement:
        if ( condition ) statement
        if ( condition ) statement else statement
        switch ( condituion ) statement
    condition:
        expression
        type-specifier-seq attribute-specifieropt declarator = initializer-clause
    

    C++11 additionally adds the following rule:

    condition:
        type-specifier-seq attribute-specifieropt declarator braced-init-list
    

    Generally it means that the declaration you might put inside the condition is in fact nothing more than keeping the value of the condition expression named for further use, i.e. for the following code:

    if (int x = 3 && true && 12 > 11) {
        // x == 1 here...
    

    x is evaluated as: 3 && true && (12 > 11).

    Back to your questions:

    3) C++11 allows you now to use direct initialization (with brace initializer) in such case, e.g:

    if (int x { 3 && true && 12 > 11 }) {
    

    4) The following: if ((int x = 1) && true) does not make sense according to the definition above as it does not fit the: “either expression or declaration” rule for the condition.

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

Sidebar

Related Questions

I have this table variable declaration followed by a query: DECLARE @CurrentItems TABLE (
This is follow up to: Android local variable get's lost when using camera intent
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
I follow this rule but some of my colleagues disagree with it and argue
Trying to follow this example. (Section String sorting...) Is there anything obvious that would
I tried to follow this but the default modelbinder let my array null on
I am trying to follow this tutorial on how to connect to a database
I am trying to follow this example (from p137 of Rob Pickering's Foundations of
I have a bunch of python methods that follow this pattern: def delete_session(guid): conn
I checked out the three20 source and was trying to follow this guide to

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.