bearophile wrote:
Yigal Chripun:
Regardless of usefulness (or good design) of such variables, this sounds
extremely dangerous. The compiler must not change semantics of the
program based on optimization. optimizing away such variables most
definitely alters the semantics.
Maybe you have misunderstood, or I have explained the things badly. So I
explain again.
I have seen that LDC (when it performs link-time optimization, that's not done
in all situations) keeps just one copy of constants inside the binary even if
such constants are present in more than one template instance. In the
situations where LTO is available I think this doesn't cause problems.
Then I am half-seriously proposing a syntax like:
T foo(T)(T x) {
static static int y;
// ...
}
Where the y is now static to (shared among) all instances of the templated
function foo. This may be a little error-prone and maybe not that useful, but
again here the compiler doesn't change the semantics of the program, because
using a double static keyword the programmer has stated such intention.
Bye,
bearophile
Oh. ok. I seems I completely misunderstood you. It wasn't clear to me
before that your were talking about constants. Of course it's perfectly
OK to optimize _constants_ like that.
IMO, static is harmful and should be avoided. some newer languages
recognize this and completely remove this from the language. I'd like to
see D going in that path rather than adding even more ways to use static.
regarding your concrete proposal - as others said, you can use global
variables for that or put this inside a struct if you want to limit the
scope.