Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 10, 2009 static static | ||||
---|---|---|---|---|
| ||||
When I convert a function to a templated function (for example because I know the value of an argument at compile time, so using a template gives me a poor's man partial compilation) the static variables get duplicated for each instance of the function template, and I may need to use true global variables/constants (but if you use link-time optimization then LDC is able to remove such shared constants). So I was thinking about a "static static" attribute that avoid moving the statics to globals. Is this a useless idea? Bye, bearophile |
November 10, 2009 Re: static static | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Hello bearophile,
> When I convert a function to a templated function (for example because
> I know the value of an argument at compile time, so using a template
> gives me a poor's man partial compilation) the static variables get
> duplicated for each instance of the function template, and I may need
> to use true global variables/constants (but if you use link-time
> optimization then LDC is able to remove such shared constants).
>
> So I was thinking about a "static static" attribute that avoid moving
> the statics to globals. Is this a useless idea?
>
> Bye,
> bearophile
you can kida cheet with a template scope variable
template Foo(T...) // unique var for each set of args.
{
int bar;
}
|
November 10, 2009 Re: static static | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile wrote:
> When I convert a function to a templated function (for example
> because I know the value of an argument at compile time, so using a
> template gives me a poor's man partial compilation) the static
> variables get duplicated for each instance of the function template,
> and I may need to use true global variables/constants (but if you use
> link-time optimization then LDC is able to remove such shared
> constants). So I was thinking about a "static static" attribute that
> avoid moving the statics to globals. Is this a useless idea?
>
> Bye, bearophile
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.
I wonder, how do other languages treat static variables inside templated functions?
|
November 10, 2009 Re: static static | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yigal Chripun | 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
|
November 10, 2009 Re: static static | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Tue, 10 Nov 2009 16:15:26 -0500, bearophile <bearophileHUGS@lycos.com> 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.
What's the advantage over:
static int y;
T foo(T)(T x) {
// ...
}
-Steve
|
November 10, 2009 Re: static static | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Tue, Nov 10, 2009 at 1:15 PM, bearophile <bearophileHUGS@lycos.com> 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.
Make it "super static" and I'm sold. :-)
Just kidding. It's never occurred to me to want something like that. I guess I'd probably just use a global variable. And maybe that's for the better since it screams out more clearly that something strange is going on. And maybe gets the developer to rethink the design one more time before going down that path.
--bb
|
November 10, 2009 Re: static static | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Steven Schveighoffer:
> What's the advantage over:
>
> static int y;
>
> T foo(T)(T x) {
> // ...
> }
That the global name space is kept clean, the "y" name can be seen only inside foo. Reducing the visibility of names is useful to keep things tidy. (Inside normal functions in D static has just that purpose).
Bye,
bearophile
|
November 10, 2009 Re: static static | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yigal Chripun | Yigal Chripun wrote:
> I wonder, how do other languages treat static variables inside templated functions?
C++ generates a different variable for each instantiation.
|
November 10, 2009 Re: static static | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile wrote:
> Steven Schveighoffer:
>
>> What's the advantage over:
>>
>> static int y;
>>
>> T foo(T)(T x) { // ... }
>
> That the global name space is kept clean, the "y" name can be seen
> only inside foo. Reducing the visibility of names is useful to keep
> things tidy. (Inside normal functions in D static has just that
> purpose).
struct MyNameSpace
{
static int y;
T foo(T)(T x) { ... }
}
|
November 11, 2009 Re: static static | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
|
Copyright © 1999-2021 by the D Language Foundation