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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:35:52+00:00 2026-06-13T02:35:52+00:00

When the same piece of code is compiled with optimizations fully disabled ( g++

  • 0

When the same piece of code is compiled with optimizations fully disabled (g++ -O0) and then again with the optimizations fully enabled (g++ -O3), how can the logic of the source code itself be changed?

For example, compilers can

  • unwind loops and
  • do constant folding.

These two optimizations make the code perform faster without affecting the integrity of the original source code. Any code that runs without these optimizations will run with them enabled.

But, compiler optimizations can also affect code logic. Here are two examples that I know of:

  • Removing copy constructors and assignment operators from temporaries may remove possible side-effects.
  • Rearranging arithmetic containing floating point values may affect floating point error (hopefully requires -ffast-math argument).

I was very surprised, and lucky, to learn about these because they could become huge potential gotchas in the wrong situation.

So I want to know, are there any other cases where c++ compiler optimizations will affect code logic? I’m specifically looking for information about c++11 (without any undefined behaviour) under the g++ compiler, but tips for other compilers are welcome.

  • 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-13T02:35:53+00:00Added an answer on June 13, 2026 at 2:35 am

    The "as-if" Rule:

    An implementation is free to disregard any requirement of this
    International Standard as long as the result is as if the requirement
    had been obeyed
    , as far as can be determined from the observable
    behavior of the program. For instance, an actual implementation need
    not evaluate part of an expression if it can deduce that its value is
    not used and that no side effects affecting the observable behavior of
    the program are produced.

    However, the standard mentions one optimization which is allowed, and which breaks the "as-if" rule:

    When certain criteria are met, an implementation is allowed to omit
    the copy/move construction of a class object, even if the copy/move
    constructor and/or destructor for the object have side effects.
    In
    such cases, the implementation treats the source and target of the
    omitted copy/move operation as simply two different ways of referring
    to the same object, and the destruction of that object occurs at the
    later of the times when the two objects would have been destroyed
    without the optimization.123 This elision of copy/move operations,
    called copy elision, is permitted in the following circumstances
    (which may be combined to eliminate multiple copies):

    — in a return
    statement in a function with a class return type, when the expression
    is the name of a non-volatile automatic object (other than a function
    or catch-clause parameter) with the same cvunqualified type as the
    function return type, the copy/move operation can be omitted by
    constructing the automatic object directly into the function’s return
    value

    — in a throw-expression, when the operand is the name of a
    non-volatile automatic object (other than a function or catch-clause
    parameter) whose scope does not extend beyond the end of the innermost
    enclosing try-block (if there is one), the copy/move operation from
    the operand to the exception object (15.1) can be omitted by
    constructing the automatic object directly into the exception object

    —
    when a temporary class object that has not been bound to a reference
    (12.2) would be copied/moved to a class object with the same
    cv-unqualified type, the copy/move operation can be omitted by
    constructing the temporary object directly into the target of the
    omitted copy/move

    — when the exception-declaration of an exception
    handler (Clause 15) declares an object of the same type (except for
    cv-qualification) as the exception object (15.1), the copy/move
    operation can be omitted by treating the exception-declaration as an
    alias for the exception object if the meaning of the program will be
    unchanged except for the execution of constructors and destructors for
    the object declared by the exception-declaration.

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

Sidebar

Related Questions

I wrote a same(..similar?) piece of code for Java 7 and C#(.net 3.5) and
I have a piece of templated code that is never run, but is compiled.
Suppose I compile a source file file which contains this piece of code, struct
The following three pieces of code achieves exactly the same effect. Yet, when compiled
I'm not sure how to explain this but this piece of code bellow can
The following three pieces of code behave exactly the same: <p {padding: 0 15
It's my understanding that if two threads are reading from the same piece of
I am trying to study shadow mapping in WebGL. I see same piece of
having same issue, i am totally new in php. i install XAMPP then download
I have this piece of code that finds words that begin with @ or

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.