Suppose my code has the following function:
inline int foo() {
bar();
int y = baz();
return y;
}
and suppose that bar()
and baz()
have side-effects.
If I write:
int z = foo();
printf("z is %d\n", z);
then obviously both bar()
and baz()
have to be executed. But if I write:
foo();
and not use the return value, the only reason to call baz()
is for its side effects.
Is there a way I can tell my compiler (for any of: GCC, clang, MSVC, Intel) something like "you may ignore the side effects of baz()
for purposes of deciding whether to optimize it away"? Or "you may ignore the side effects of this instruction for purposes of deciding whether to optimize it away"?
This question is phrased for C and C++ but could be relevant for all sorts of procedural languages. Let's make it C++ because that's what I use the most right now.
Note:
bar()
,baz()
are not under my control.- The signature of
foo()
may not be altered. Of course I could use some proxy and applybaz()
lazily, if I so wanted; or just write a different function for the case of not usingy
. That's not what I'm asking.