[Issue 23987] Copy construction should not disable implicit conversion

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jun 19 13:42:41 UTC 2023


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

RazvanN <razvan.nitu1305 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305 at gmail.com

--- Comment #2 from RazvanN <razvan.nitu1305 at gmail.com> ---
I don't think this bug report is valid. Since a copy constructor is defined for
S, then all copies that are being made for it need to call a copy constructor.
Since the copy constructor is only defined for mutable destinations, I don't
see how we could safely support the automatic conversion from mutable to
immutable. That would open the door for code like this one:

int* s;

struct S
{
    int p;

    this(const ref S src)
    {
        s = &p;
    }
}

void main()
{
    immutable(S) s1 = S(2);
    immutable(S) s2 = s1;
    writeln(*s2.p);
    *s = 9;
    writeln(*s2.p);
}

--


More information about the Digitalmars-d-bugs mailing list