468,241 Members | 1,678 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,241 developers. It's quick & easy.

OK for compiler to not call function with constant return?

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
Feb 9 '06 #1
4 992
Hi,
It wont assign false directly. It will only happen if the function
is inline.

Best regards,
Amal P.

Feb 9 '06 #2
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...
Feb 9 '06 #3
In message <lD**********************@news.xtra.co.nz>, Aaron Lawrence
<aa*******@HEREintegration.co.nz> 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
Feb 9 '06 #4
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
Feb 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by r5 | last post: by
20 posts views Thread by Markus Sandheide | last post: by
43 posts views Thread by Anitha | last post: by
14 posts views Thread by joshc | last post: by
9 posts views Thread by Brian Tyler | last post: by
29 posts views Thread by Ark | last post: by
reply views Thread by erik.erikson | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.