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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:38:20+00:00 2026-06-11T12:38:20+00:00

I was reading Stroustrup’s The C++ Programming Language, where he says that out of

  • 0

I was reading Stroustrup’s “The C++ Programming Language”, where he says that
out of two ways to add something to a variable

x = x + a;

and

x += a;

He prefers += because it is most likely better implemented. I think he means that it works faster too.
But does it really? If it depends on the compiler and other things, how do I check?

  • 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-11T12:38:21+00:00Added an answer on June 11, 2026 at 12:38 pm

    Any compiler worth its salt will generate exactly the same machine-language sequence for both constructs for any built-in type (int, float, etc) as long as the statement really is as simple as x = x + a; and optimization is enabled. (Notably, GCC’s -O0, which is the default mode, performs anti-optimizations, such as inserting completely unnecessary stores to memory, in order to ensure that debuggers can always find variable values.)

    If the statement is more complicated, though, they might be different. Suppose f is a function that returns a pointer, then

    *f() += a;
    

    calls f only once, whereas

    *f() = *f() + a;
    

    calls it twice. If f has side effects, one of the two will be wrong (probably the latter). Even if f doesn’t have side effects, the compiler may not be able to eliminate the second call, so the latter may indeed be slower.

    And since we’re talking about C++ here, the situation is entirely different for class types that overload operator+ and operator+=. If x is such a type, then — before optimization — x += a translates to

    x.operator+=(a);
    

    whereas x = x + a translates to

    auto TEMP(x.operator+(a));
    x.operator=(TEMP);
    

    Now, if the class is properly written and the compiler’s optimizer is good enough, both will wind up generating the same machine language, but it’s not a sure thing like it is for built-in types. This is probably what Stroustrup is thinking of when he encourages use of +=.

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

Sidebar

Related Questions

I'm reading The C++ Programming Language and in it Stroustrup states that the int
I'm reading The C++ Programming Language. In it Stroustrup states that sizeof(char) == 1
I'm reading Stroustrup's Programming: Principles and Practice and came across this code: int main()
I am reading Bjarne Stroustrup's Programming : Principles and Practice Using C++ In the
Reading C++ Templates: The Complete Guide and it says Note that templates cannot be
I was reading Stroustrup's c++ FAQ and I noticed that, at one point, he
Reading the VS2008 help file I've figured out that the clean way of exiting
Reading the book VS 2010 by John Sharp, it says that generics allows to
I was recently reading Stroustrups The C++ Programming Language and in the section about
I'm reading Stroustrup's book, the section on overloading and related ambiguities. There's an example

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.