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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:56:08+00:00 2026-05-24T02:56:08+00:00

Consider following statement: C a, b; //C contains c1, c2 and c3 all integers

  • 0

Consider following statement:

 C a, b; //C contains c1, c2 and c3 all integers

if(a.c1==b.c1 && a.c2 == b.c2) {
     a.c3=b.c3;
 }

Will this statement be optimized to the following:

 if(a.c1 == b.c1) {
    if(a.c2 == b.c2) {
       a.c3=b.c3;
    }
 }

AFAIK, C++ compilers does not perform this kind of operation since it can have side effects. But these are built-in types.

  • Is there anything related in the standard?
  • If it is compiler specific is main stream compilers (MS, GNU, Intel) are doing it or not?
  • 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-24T02:56:08+00:00Added an answer on May 24, 2026 at 2:56 am

    Yes. The following code snippet:

    C a, b; //C contains c1, c2 and c3 all integers
    if(a.c1==b.c1 && a.c2 == b.c2)
    {
        a.c3=b.c3;
    }
    

    will be “optimized” to this (or something equivalent):

    if(a.c1 == b.c1)
    {
        if(a.c2 == b.c2)
        {
            a.c3=b.c3
        }
    }
    

    This is required not because of optimization but because the C++ standard requires short-circuit evaluation. So reasonably standards-conforming C++ compilers should be able to short-circuit that.

    There isn’t a single place in the C++ standard that explicitly states that some boolean operators are short-circuited. It is implied from the rules:

    ISO/IEC C++ Standard 14882 §5.14 Logical AND operator [expr.log.and]

    logical-and-expression:
        inclusive-or-expression
        logical-and-expression && inclusive-or-expression
    
    1. The && operator groups left-to-right. The operands are both
      implicitly converted to type bool (clause 4). The result is true if
      both operands are true and false otherwise. Unlike &, &&
      guarantees left-to-right evaluation: the second operand is not
      evaluated if the first operand is false.

    The rules are similar for the || operator:

    ISO/IEC C++ Standard 14882 §5.15 Logical OR operator [expr.log.or]

    logical-or-expression:
        logical-and-expression
        logical-or-expression || logical-and-expression
    
    1. The || operator groups left-to-right. The operands are both
      implicitly converted to bool (clause 4). It returns true if either
      of its operands is true, and false otherwise. Unlike |, ||
      guarantees left-to-right evaluation; moreover, the second operand is
      not evaluated if the first operand evaluates to true.

    And the conditional ? operator:

    ISO/IEC C++ Standard 14882 §5.16 Conditional operator [expr.cond]
    conditional-expression:
    logical-or-expression
    logical-or-expression ? expression : assignment-expression

    1. Conditional expressions group right-to-left. The first expression
      is implicitly converted to bool (clause 4). It is evaluated and if
      it is true, the result of the conditional expression is the value of
      the second expression, otherwise that of the third expression. All
      side effects of the first expression except for destruction of
      temporaries (12.2) happen before the second or third expression is
      evaluated. Only one of the second and third expressions is
      evaluated.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider the following code snippet: #!/bin/bash #This program tests the if statement in bash.
Consider the following 2 queries: select tblA.a,tblA.b,tblA.c,tblA.d from tblA where tblA.a not in (select
consider the following pgSQL statement: SELECT DISTINCT some_field FROM some_table WHERE some_field LIKE 'text%'
Consider the following switch statement: switch( value ) { case 1: return 1; default:
Consider the following SQL statement: SELECT invoice.customer_name, invoice.customer_id, invoice.shop_location, sum(invoice.cost) FROM invoice GROUP BY
In following code consider the statement :- int B::*bpm; class B { public :
Consider the following TSQL: declare @xml xml select @xml = '<test xmlns=http://this-is-the-default-namespace-uri>some data</test>' select
Consider the following situation: There is an xml that contains data @XmlData The @XmlData
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)

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.