[Issue 13488] implicit conversions to immutable broken

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Sep 17 04:07:04 PDT 2014


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

Kenji Hara <k.hara.pg at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[reg] implicit conversions  |implicit conversions to
                   |to immutable broken         |immutable broken

--- Comment #2 from Kenji Hara <k.hara.pg at gmail.com> ---
There's 

>     struct T {
>       const(int)* p = null;
>       this(const(int)* q) pure {
>         p = q;
>       }
>     }

When we see only the signature (const(int*)) pure, the constructor cannot
construct immutable object in general, because the parameter q may receive
mutable object.

If you want to restrict constructed object qualifier by using constructor
argument qualifiers, you can use inout constructor.

struct T
{
    const(int)* p = null;
    this(inout(int)* q) inout// pure
    {
        p = q;
    }
}

void foo()
{
    int x;
    T mt1 = T(&x);            // ok
    T* mp1 = new T(&x);       // ok
  //T mt2 = immutable T(&x);            // NG
  //T* mp2 = new immutable T(&x);       // NG
    immutable int y;
  //immutable(T) it1 = T(&y);       // NG
  //immutable(T)* ip1 = new T(&y);  // NG
    immutable(T) it2 = immutable T(&y);      // ok
    immutable(T)* ip2 = new immutable T(&y); // ok
}

--


More information about the Digitalmars-d-bugs mailing list