WTF! new in class is static?!?!
ag0aep6g
anonymous at example.com
Thu Jun 7 22:02:18 UTC 2018
On 06/07/2018 11:26 PM, Ethan wrote:
> The spec isn't clear on this but it uses the same rules as struct field
> initialisation, ie it's defined once and copied to each instance on
> creation.
>
> https://dlang.org/spec/struct.html#default_struct_init
It says there that "The default initializers may not contain references
to mutable data."
DMD enforces that for pointers to primitives, but not for arrays, class
objects, or even pointers to structs:
----
struct S
{
int* p = new int(1); /* Error: cannot use non-constant CTFE pointer
in an initializer */
int[] a = [1, 2, 3]; /* no error */
C c = new C; /* no error */
S2* s = new S2; /* no error */
}
class C { int x = 4; }
struct S2 { int x = 5; }
----
Seems inconsistent to me.
More information about the Digitalmars-d-learn
mailing list