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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:16:33+00:00 2026-06-14T05:16:33+00:00

I have read this article: Undefined behavior and sequence points , but i cannot

  • 0

I have read this article: Undefined behavior and sequence points, but i cannot figure, whether it is UB, or not.

Consider, following example:

#include <iostream>
class op {
public:
  explicit op(int x) {
    std::cout << "x:   " << x << std::endl;
  }

  op & operator + (const op & /* other */) {
    return *this;
  }
};

int main(int /* argc */, char * /* argv */ []) {
  int x = 0;
  op o = op(x++) + op(x++) + op(x++);

  std::cout << "res: " << x << std::endl;

  return 0;
}

I expect output like this (or some permutation of output based on order of evaluation):

x:   0
x:   1
x:   2
res: 3

gcc-4.7.1 and clang-3.0 gives me such output, but when i compile this example with msvc-2010 i’ve got output:

x:   0
x:   0
x:   0
res: 3

Can you give me some information about this behaviour.

  • 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-14T05:16:35+00:00Added an answer on June 14, 2026 at 5:16 am

    The order of arguments evaluation in a + b + c is compiler-specific. Thus, the order of calls to x++ will be compiler-specific and relaying on it will be undefined behavior.

    Using x++ or++x in such expressions is usually a sign of bad coding standard. It is better to avoid it and simplify your expressions.

    In this question Compilers and argument order of evaluation in C++ you can find a discussion on the order of arguments evaluation in C++.

    Here is the explanation of C++ evaluation order with the references to C++ standard: http://en.cppreference.com/w/cpp/language/eval_order

    P.S. Bjarne Stroustrup says it explicitly in “The C++ Programming Language” 3rd edition section 6.2.2. He also gives a reason:

    Better code can be generated in the absence of restrictions on expression evaluation order.

    (from https://stackoverflow.com/a/2934909/1065190)

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

Sidebar

Related Questions

On the following website they have the following section People who read this article
I have read this article on Codeproject, but I don't know how to apply
i've read this article about authentication library. some of this answer is not satisfy
I have read this article about using WMI to change the settings of NICs
I'm developing a program for iPhone. I have read this article and I have
i have read this article ScuttGu for making use of User control to make
i have read this article from dev.mysql . in that page is a example
I have a silly question. I read this article about std::exception http://www.cplusplus.com/doc/tutorial/exceptions/ On catch
I have read through Scott Hanselman's post on this topic. I found another article
I have read this article and used it to write my first Ant task

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.