Inability to dup/~ for const arrays of class objects
Ali Çehreli
acehreli at yahoo.com
Tue May 28 17:08:44 PDT 2013
On 05/28/2013 04:32 PM, Peter Williams wrote:
> I find that dup works for const T[] when T is not a class (although I
> haven't tried it with pointers). This means I write two versions of my
> code - one for classes (which does (cast(T[])).dup) and one for the rest.
There is current and related thread on the D.learn forum:
http://forum.dlang.org/post/bsbhpdgcpfmkvsclsskq@forum.dlang.org
I think it is unnecessarily restrictive that a const class variable
cannot refer to another class object:
class C
{}
void main()
{
const(C) c;
c = new const(C); // <-- compilation error
}
I think this limitation is at the core of your issue as well: .dup
creates mutable objects but your array is not able to contain const
class variables to refer to those mutable objects. I think it should be
possible.
I have a feeling that this may be related to that surprising issue with
C and C++:
http://www.parashift.com/c++-faq-lite/constptrptr-conversion.html
The reason I suspect so is because a class variable is one level of
indirection and a slice is another level of indirection. Does that make
this issue the same as the C++ issue? If not, perhaps the implementation
of such a limitation is the cause of this bug. (?)
Ali
More information about the Digitalmars-d
mailing list