[Issue 4251] Hole in the const system: immutable values can be overwritten (const(T) is appendable to const(T)[])

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jun 16 13:23:14 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=4251



--- Comment #11 from Stewart Gordon <smjg at iname.com> 2011-06-16 13:18:31 PDT ---
(In reply to comment #10)
> Yah, this has constantly puzzled starting C++ programmers - you can convert
> char* to const(char*) but not char** to const(char*)*.

Do you mean char** to const(char)** ?

> Generally, consider types P (permissive) and N (nonpermissive). Assume both
> types have the same structure, so there is no matter of converting
> representation. Generally you can't convert the address of a N to the address
> of a P even if you can actually convert a N to an P. This is because the
> address conversion would allow you subsequent P-specific operations directly
> against an N object.

Well said.

Converting T* (N) to const(T)* (P) is safe.
The P-specific operation is rebinding it to an immutable(T).
So converting T** to const(T)** is unsafe.

Similarly,
Converting immutable(T)* (N) to const(T)* (P) is safe.
The P-specific operation is rebinding it to a mutable T.
So converting immutable(T)** to const(T)** is unsafe.

This is the principle that underlies all these proposed rules - whether the
indirection is a pointer, dynamic array or other container type, and whether
the N->P is a constancy change or a walk up the class hierarchy.

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


More information about the Digitalmars-d-bugs mailing list