www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8288] New: immutable(char)** is not convertable to const(char)**

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8288

           Summary: immutable(char)** is not convertable to const(char)**
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: admin dav1d.de



This Code:

void foo(const(char)** arg) {}
void main() {
    string yay = "fun";
    auto yay_data = yay.ptr;

    foo(&yay_data);
}

Produces this Error:

Error: function compileme406.foo (const(char)** arg) is not callable using
argument types (immutable(char)**)
Error: cannot implicitly convert expression (& yay_data) of type
immutable(char)** to const(char)**

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 23 2012
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8288


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID



This is design. The compiler rejects such conversion to keep const-correctness
will be broken.

See following test case.

----
void main()
{
    string yay = "fun";
    immutable(char)*  ip = yay.ptr;
    immutable(char)** ipp = &ip;

    // Implicit conversion from immutbale(char)** to const(char)** is
    // not allowed. But if this is allowed...?
    const(char)** cpp = ipp;

    char[] mstr = ['a', 'b', 'c'];
    char* mp = mstr.ptr;
    *cpp = mp;
        // You can rewrite a value of pointer to immutable data
        // as a value of pointer to mutable data!
    assert(cast(void*)ip is cast(void*)mp);
        // breaking const correctness!

    assert(*ip == 'a');
    mstr[0] = 'x';
    assert(*ip == 'x'); // immutable data is rewritten!!
}

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