OK for compiler to not call function with constant return?

A

Aaron Lawrence

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
 
A

Amal P

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

Best regards,
Amal P.
 
B

Ben Pope

Aaron said:
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
 
R

Richard Herring

Aaron Lawrence said:
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?
 
A

Aaron Lawrence

Richard said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top