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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Sep 24 23:35:23 PDT 2012


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


monarchdodra at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra at gmail.com


--- Comment #19 from monarchdodra at gmail.com 2012-09-24 23:36:12 PDT ---
Erm, it was my understanding that @disable this() meant that it becomes illegal
to initialize a struct to *just* .init (or call the .init property directly),
and that a constructor must be called. The .init property still exists though,
because construction can't actually happen without .init anyways...

------
import std.stdio;
struct S
{
    int i;
    S(int v)
    {
        i+=v;
    }
}

void main()
{

    // S s1; //Error: variable main.main.s1 initializer required for type S
    S s3 = S(3);
    writeln(s3.i);
}
------
3
------
The fact that this program outputs three is proof that s3 is first .init
initialized, before the constructor is called proper. That's the only logical
behavior (IMO) when you think of how D's initialization scheme works. If .init
was truly disabled, the ONLY legal initialization would become:
----
S s = void;
----


THIS works though :/ ???
--------
void main()
{
    S s2 = S(); //WHAT...?
    S s3 = S(3);
    writeln(s3.i);
}
--------
I side with Jonathan that S() and .init should be the same thing. There are no
"default constructors" (or "no-arg", to my great dismay), in D, so having "S s
= S();" do some things behind the scenes that "S s;" doesn't is just not
conceivable from a language standpoint.

The fact that the stack pointer is not known at compile time in .init (I think,
anyways) should not prevent "S s;" from properly initialized, be it by a
stwo-step scheme if needed. That is a compiler implementation details, and
users should (NEED) to remain blissfully unaware of it.

Either that, or we allow a true constructor that takes no argument:
S s; // .init
S s = S(); // this(){...}

But as of right now, it would seem there is a bastard of an hybrid that is half
of both.

-- 
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