Re: May a compiler optimize a reference to constant parameter to
constant value ?
On Oct 13, 9:38 am, Good Guy <pooria...@gmail.com> wrote:
Consider following function:
void func(const char & input){
//do something
}
Apparently it makes sense for the parameter to be constant
value not reference to constant, Now may a compiler optimize
that to constant value so that it'll be the same as following
?
void func(const char input){
//do something
}
The compiler can do pretty much anything it likes, as long as
the observable behavior of the program doesn't change.
In practice, the compiler can only change the defined calling
interface from the standard form if it can find all of the call
sites, and change them as well (possibly inlining the function
and eliminating the argument entirely). This is generally
possible if the function is static, and so isn't visible outside
the translation unit, or if the function is inline, but modern
compilers are capable of optimizing accross translation unit
boundaries, and may be capable of finding all of the call sites
in the entire program. And as Keith Duggar has said, it also
depends on what the function does. Taking the address of
a reference returns the address of what is referred to, and if
this affects the observable behavior of the program, then the
compiler must take that into account.
--
James Kanze