Bug 2146 – Multiple execution of 'static this' defined in template
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2008-06-09T08:07:00Z
Last change time
2014-02-24T15:31:33Z
Assigned to
bugzilla
Creator
samukha
Comments
Comment #0 by samukha — 2008-06-09T08:07:52Z
File a:
----
module a;
import b;
alias Foo!(int) foo;
void main()
{
}
----
File b:
----
module b;
import std.stdio;
template Foo(T)
{
T x;
static this()
{
writefln("Initing " ~ T.stringof);
T x = T.init; // less trivial initialization here.
}
}
void baz()
{
alias Foo!(int) bar;
}
----
Output:
Initing int
Initing int
Note that the constructor is executed only once if we move 'alias Foo!(int) bar;' out of baz() to the scope of module b.
Comment #1 by samukha — 2008-06-09T08:22:03Z
'T x = T.init;' in the template should read 'x = T.init;', sorry:
File a:
----
module a;
import b;
alias Foo!(int) foo;
void main()
{
}
----
File b:
----
module b;
import std.stdio;
template Foo(T)
{
T x;
static this()
{
writefln("Initing " ~ T.stringof);
x = T.init; // less trivial initialization here.
}
}
void baz()
{
alias Foo!(int) bar;
}
----