www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18719] New: Doubly-called constructor against member when

https://issues.dlang.org/show_bug.cgi?id=18719

          Issue ID: 18719
           Summary: Doubly-called constructor against member when using
                    forwarding constructors
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: andrei erdani.com

Consider:

struct S
{
    int x = -1;
    this(int y) immutable
    {
        x = y;
        import std.stdio;
        writeln("Ctor called with ", y);
    }
    void opAssign(int) immutable;
}

class C
{
    S x;
    this() immutable
    {
        this(42); /* Initializes x. */
        x = 13; /* Breaking immutable, or ok? */
    }
    this(int x) immutable
    {
        this.x = x;
    }
}

void main()
{
    new immutable C;
}

The code prints:

Ctor called with 42
Ctor called with 13

Constructor should not be called twice against the same object.

--
Apr 03 2018