Static constructors guaranteed to run?

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 28 19:31:18 PDT 2015


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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150629/1a078775/attachment.sig>


More information about the Digitalmars-d-learn mailing list