[Issue 7021] Structs with disabled default constructors can be constructed without calling a constructor.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Sep 21 06:25:15 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=7021



--- Comment #4 from Kenji Hara <k.hara.pg at gmail.com> 2012-09-21 06:26:06 PDT ---
(In reply to comment #1)
> This works just fine too (with dmd 2.060HEAD)
> 
> struct Foo
> {
>     @disable this();
> }
> 
> void main()
> {
>     auto foo = Foo.init;
> }
> 
> It looks to me like @disable this() isn't working at all.

I think that built-in init property should be valid even if default ctor is
disabled.
T.init shows runtime object initial bit-wise representation. So, in this case,
Foo.init would be 1 byte zero filled memory, and should be accessible even if
Foo is not default constructible.

But T.init sometimes does not *valid* object. See following example.

struct Bar
{
    int value;

    @disable this();
    this(int v) { assert(v > 0); value = v; }
}

Bar's constructor appeals that Bar.value is initialized with natural number.
But Bar.init.value == 0, because it doesn't call valid ctor. Then T.init is
*invalid* object.
This is not a defect of D language, but also it is a point you should be
careful.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list