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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:15:47+00:00 2026-06-04T05:15:47+00:00

In C++, does the following have undefined behaviour: int i = 0; (i+=10)+=10; There

  • 0

In C++, does the following have undefined behaviour:

int i = 0;
(i+=10)+=10;

There was some debate about this in the comments to my answer to What's the result of += in C and C++? The subtlety here is that the default response seems to be “yes”, whereas it appears that the correct answer is “it depends on the version of the C++ standard”.

If it does depend on the version of the standard, please explain where it’s UB and where it’s 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-06-04T05:15:51+00:00Added an answer on June 4, 2026 at 5:15 am

    tl;dr: The sequence of the modifications and reads performed in (i+=10)+=10 is well defined in both C++98 and C++11, however in C++98 this is not sufficient to make the behavior defined.

    In C++98 multiple modifications to the same object without an intervening sequence-point results in undefined behavior, even when the order of those modifications is well specified. This expression does not contain any sequence points and so the fact that it consists of two modifications is sufficient to render its behavior undefined.

    C++11 doesn’t have sequence points and only requires that the modifications of an object be ordered with respect to each other and to reads of the same object to produce defined behavior.

    Therefore the behavior is undefined in C++98 but well defined in C++11.


    C++98

    C++98 clause [expr] 5 p4

    Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expression, and the order in which side effects take place, is unspecified.

    C++98 clause [expr.ass] 5.17 p1

    The result of the assignment operation is the value stored in the left operand after the assignment has taken place; the result is an lvalue

    So I believe the order is specified, however I don’t see that that alone is enough to create a sequence point in the middle of an expression. And continuing on with the quote of [expr] 5 p4:

    Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression.

    So even though the order is specified it appears to me that this is not sufficient for defined behavior in C++98.


    C++11

    C++11 does away sequence points for the much clearer idea of sequence-before and sequenced-after. The language from C++98 is replaced with

    C++11 [intro.execution] 1.9 p15

    Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. […]

    If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.

    C++11 [expr.ass] 5.17 p1

    In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression.

    So while being ordered was not sufficient to make the behavior defined in C++98, C++11 has changed the requirement such that being ordered (i.e., sequenced) is sufficient.

    (And it seems to me that the extra flexibility afforded by ‘sequence before’ and ‘sequenced after’ has lead to a much more clear, consistent, and well specified language.)


    It seems unlikely to me that any C++98 implementation would actually do anything surprising when the sequence of operations is well specified even if that is insufficient to produce technically well defined behavior. As an example, the internal representation of this expression produced by Clang in C++98 mode has well defined behavior and does the expected thing.

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

Sidebar

Related Questions

I have a thread that does the following: 1) Do some work 2) Wait
I have the following script which does not work 100%, it returns about 20
I have a file transfer program. The program (Client) does following operations to send
I have following code that does not work due to a being a value
I have following code that does not work: I never get to goToFoodDetail .
I currently have code that does the following: private final static ExecutorService pool =
I have a build process that does the following: Runs TLBIMP on a number
I have a PHP script that does the following: Uses file_get_contents() to get content
I have a daemon that does the following retrieves site members from a mysql
I have a C# windows application which does the following: 1) the main form

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.