Bug 8288 – immutable(char)** is not convertable to const(char)**

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-06-23T04:00:00Z
Last change time
2012-06-23T21:42:07Z
Keywords
rejects-valid
Assigned to
nobody
Creator
admin

Comments

Comment #0 by admin — 2012-06-23T04:00:05Z
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)**
Comment #1 by k.hara.pg — 2012-06-23T21:42:07Z
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!! }