OK for compiler to not call function with constant return?

Discussion in 'C++' started by Aaron Lawrence, Feb 9, 2006.

  1. Hi all.

    Suppose I have

    bool CFoo::AMethod()
    {
    // do lots of side effects...
    ...
    return false; // the only return
    }

    and later I have:

    bool result = AMethod();

    Is it OK for the compiler to skip the call to AMethod and just assign
    the value false to result? Even if all optimisations are off?

    Thanks

    Aaron
     
    Aaron Lawrence, Feb 9, 2006
    #1
    1. Advertising

  2. Aaron Lawrence

    Amal P Guest

    Hi,
    It wont assign false directly. It will only happen if the function
    is inline.

    Best regards,
    Amal P.
     
    Amal P, Feb 9, 2006
    #2
    1. Advertising

  3. Aaron Lawrence

    Ben Pope Guest

    Aaron Lawrence wrote:
    > Hi all.
    >
    > Suppose I have
    >
    > bool CFoo::AMethod()
    > {
    > // do lots of side effects...
    > ...
    > return false; // the only return
    > }
    >
    > and later I have:
    >
    > bool result = AMethod();
    >
    > Is it OK for the compiler to skip the call to AMethod and just assign
    > the value false to result? Even if all optimisations are off?


    Not in general, it depends what you mean by side-effects.

    Can you post complete, compilable code that demonstrates the problem?

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Feb 9, 2006
    #3
  4. In message <lDEGf.139500$>, Aaron Lawrence
    <> writes
    >Hi all.
    >
    >Suppose I have
    >
    >bool CFoo::AMethod()
    >{
    > // do lots of side effects...
    > ...
    > return false; // the only return
    >}
    >
    >and later I have:
    >
    >bool result = AMethod();
    >
    >Is it OK for the compiler to skip the call to AMethod and just assign
    >the value false to result? Even if all optimisations are off?
    >

    Not if the side-effects affect program state. The compiler is allowed to
    eliminate some calls to copy constructors, but otherwise the as-if rule
    means that if the function has detectable side-effects, they must take
    place.

    Can you post a short complete program that demonstrates your actual
    problem?

    --
    Richard Herring
     
    Richard Herring, Feb 9, 2006
    #4
  5. Richard Herring wrote:
    > Not if the side-effects affect program state. The compiler is allowed to
    > eliminate some calls to copy constructors, but otherwise the as-if rule
    > means that if the function has detectable side-effects, they must take
    > place.


    That's what I thought. It seemed like a clear case of the optimisation
    breaking my reasonable code because I had accidentally returned false in
    all cases. Suddenly my function was no longer called.

    > Can you post a short complete program that demonstrates your actual
    > problem?


    Should be possible. Tomorrow ...

    Cheers

    Aaron
     
    Aaron Lawrence, Feb 9, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,126
  2. Martin Magnusson
    Replies:
    2
    Views:
    525
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    584
    Tim Roberts
    Nov 23, 2006
  4. No Spam
    Replies:
    7
    Views:
    587
    Micah Cowan
    Jan 4, 2005
  5. Replies:
    4
    Views:
    352
    Keith Thompson
    Dec 14, 2006
Loading...

Share This Page