Thread overview | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 26, 2015 Static constructors guaranteed to run? | ||||
---|---|---|---|---|
| ||||
Are static constructors guaranteed to run if the module is imported? Also are static constructors in templated types guaranteed to run for every instantiation? Even if the instantiation is never actually used outside of compile time code, like in an alias or in a UDA? |
June 27, 2015 Re: Static constructors guaranteed to run? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja | On Friday, 26 June 2015 at 22:00:54 UTC, Tofu Ninja wrote: > Are static constructors guaranteed to run if the module is imported? AFAIK, yes. > Also are static constructors in templated types guaranteed to run for every instantiation? Even if the instantiation is never actually used outside of compile time code, like in an alias or in a UDA? Definitely not. Things inside a template don't even exist if that template is never instantiated. |
June 27, 2015 Re: Static constructors guaranteed to run? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On 06/27/2015 11:54 AM, "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm@gmx.net>" wrote:
>
>
>> Also are static constructors in templated types guaranteed to run for
>> every instantiation? Even if the instantiation is never actually used
>> outside of compile time code, like in an alias or in a UDA?
>
> Definitely not. Things inside a template don't even exist if that
> template is never instantiated.
(That wasn't his question.)
|
June 27, 2015 Re: Static constructors guaranteed to run? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja Attachments: | On Fri, 26 Jun 2015 22:00:51 +0000, Tofu Ninja wrote:
> Are static constructors guaranteed to run if the module is imported? Also are static constructors in templated types guaranteed to run for every instantiation? Even if the instantiation is never actually used outside of compile time code, like in an alias or in a UDA?
i believe that the answers are:
1. yes.
2. no.
|
June 27, 2015 Re: Static constructors guaranteed to run? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Saturday, 27 June 2015 at 22:20:40 UTC, ketmar wrote:
> 2. no.
Hmm... any reason why?
|
June 28, 2015 Re: Static constructors guaranteed to run? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Saturday, 27 June 2015 at 20:16:10 UTC, Timon Gehr wrote:
> On 06/27/2015 11:54 AM, "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm@gmx.net>" wrote:
>>
>>
>>> Also are static constructors in templated types guaranteed to run for
>>> every instantiation? Even if the instantiation is never actually used
>>> outside of compile time code, like in an alias or in a UDA?
>>
>> Definitely not. Things inside a template don't even exist if that
>> template is never instantiated.
>
> (That wasn't his question.)
Ah sorry, I see. In this case I don't know the answer. There might even be a difference between normal CTFE code and is-expressions...
|
June 29, 2015 Re: Static constructors guaranteed to run? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja Attachments: | On Sat, 27 Jun 2015 22:49:13 +0000, Tofu Ninja wrote:
> On Saturday, 27 June 2015 at 22:20:40 UTC, ketmar wrote:
>> 2. no.
>
> Hmm... any reason why?
if instantiated template was not used in any code that makes into compiled binary, compiler is free to remove it with all it's ctors. it may do that, or may not, but removal is allowed. so while that can work now (i didn't checked), it may stop working in next version (or with another compiler), and that will not be a bug.
|
June 29, 2015 Re: Static constructors guaranteed to run? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja Attachments: | On Sat, 27 Jun 2015 22:49:13 +0000, Tofu Ninja wrote:
> On Saturday, 27 June 2015 at 22:20:40 UTC, ketmar wrote:
>> 2. no.
>
> Hmm... any reason why?
p.s. note that static ctors are *intended* to run in runtime, not in compile time. if compiler decides that some code is not required in runtime, it is free to remove all that code, including static ctors.
|
June 29, 2015 Re: Static constructors guaranteed to run? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Monday, 29 June 2015 at 02:07:57 UTC, ketmar wrote:
> On Sat, 27 Jun 2015 22:49:13 +0000, Tofu Ninja wrote:
>
>> On Saturday, 27 June 2015 at 22:20:40 UTC, ketmar wrote:
>>> 2. no.
>>
>> Hmm... any reason why?
>
> if instantiated template was not used in any code that makes into compiled binary, compiler is free to remove it with all it's ctors. it may do that, or may not, but removal is allowed. so while that can work now (i didn't checked), it may stop working in next version (or with another compiler), and that will not be a bug.
Can you say the same about non templated static constructors? Like if a type is never used but it has a static constructor, is the compiler free to remove that as well?
|
June 29, 2015 Re: Static constructors guaranteed to run? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tofu Ninja Attachments: | On Mon, 29 Jun 2015 02:19:54 +0000, Tofu Ninja wrote:
> On Monday, 29 June 2015 at 02:07:57 UTC, ketmar wrote:
>> On Sat, 27 Jun 2015 22:49:13 +0000, Tofu Ninja wrote:
>>
>>> On Saturday, 27 June 2015 at 22:20:40 UTC, ketmar wrote:
>>>> 2. no.
>>>
>>> Hmm... any reason why?
>>
>> if instantiated template was not used in any code that makes into
>> compiled binary, compiler is free to remove it with all it's ctors. it
>> may do that, or may not, but removal is allowed.
>> so while that can work now (i didn't checked), it may stop working in
>> next version (or with another compiler), and that will not be a bug.
>
> Can you say the same about non templated static constructors? Like if a type is never used but it has a static constructor, is the compiler free to remove that as well?
yes. it doesn't do that now, afair, but i can't see any sense in running code that obviously does nothing, as it's owner is not used. module ctors was designed for such things -- i.e. to run some code on startup. if someone is doing some vital initialization in static ctor of struct or class, and that struct or class aren't used anywhere else in his code, he's doing it wrong. it *may* work now, but that's simply 'cause compiler is not very well at removing unused code.
|
Copyright © 1999-2021 by the D Language Foundation