Re: optimize const T& parameter to T
On Thu, 22 Feb 2007 08:57:31 CST, "W Karas" <wkaras@yahoo.com> wrote:
On Feb 22, 1:42 am, clamsd-n...@yahoo.com wrote:
On Tue, 20 Feb 2007 12:05:50 CST, "W Karas" <wka...@yahoo.com> wrote:
On Feb 16, 7:48 pm, stephen.clam...@sun.com wrote:
On Fri, 16 Feb 2007 17:23:08 CST, "W Karas" <wka...@yahoo.com> wrote:
Consider a source file that has (only) this:
template <typename T>
void foo(const T &i);
void bar(int i) { foo(i); }
If it was compiled with optimization, is it unreasonable to expect it
should generate the same object code (aside from differences in
mangled names) as this file?
The compiler is in general not allowed to make that "optimization".
Why do you put optimization in quotes? What I suggest would
make the resulting program smaller and faster.
But as I showed later, the optimization is not in general valid.
Sorry, I don't see that. The optimization is valid I think for T a
primitive type or a class with no direct or indirect mutable
data members.
int k = 1;
void foo();
int f(const int& i)
{
foo();
cout << i;
}
/*
int g(int i)
{
foo();
cout << i;
}
*/
Your claim is that the compiler can transform the definition of f into
the function g, and change call sites accordingly.
Suppose we call f(k) and foo() modifies k. The modified and unmodified
versions of f have different observable behavior, so the
transformation is not valid. In this example, the compiler does not
know whether foo modifies k, or whether f will be called with k as an
argument.
As I said before, with whole-program analysis, the compiler can in
principle find call sites where the tranformation is valid, and
generate multiple versions of a function with reference parameters,
calling the appropriate version based on what it can prove is a safe
transformation.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]