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. Advertisements

  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. Advertisements

  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. Advertisements

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. No Spam
    Replies:
    7
    Views:
    769
    Micah Cowan
    Jan 4, 2005
  2. Greenhorn
    Replies:
    15
    Views:
    1,134
    Keith Thompson
    Mar 6, 2005
  3. grbgooglefan
    Replies:
    2
    Views:
    647
    Pascal Bourguignon
    Jan 30, 2008
  4. grbgooglefan
    Replies:
    4
    Views:
    672
    Kenny McCormack
    Jan 30, 2008
  5. grbgooglefan
    Replies:
    0
    Views:
    571
    grbgooglefan
    Jan 30, 2008
  6. Alok
    Replies:
    3
    Views:
    503
  7. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    476
    THAKUR PRASHANT SINGH
    Feb 27, 2010
  8. Replies:
    9
    Views:
    308
Loading...