Copying structs with pointers
Peter Alexander
peter.alexander.au at gmail.com
Wed Jul 20 15:30:27 PDT 2011
On 20/07/11 11:06 PM, Jonathan M Davis wrote:
> Postblit doesn't work with const or immutable right now:
> http://d.puremagic.com/issues/show_bug.cgi?id=4867
Is that the issue though? I believe it's a deeper issue.
An example:
struct Ptr
{
int* p;
this(int x) { p = new int; *p = x; }
this(this) { /+ do nothing +/ } // (!!!)
const int get() { return *p; }
void set(int x) { *p = x; }
}
void main()
{
const Ptr a = Ptr(1);
Ptr b = a; // shallow copy! const removed!
b.set(2);
assert(a.get() == 1); // FAIL
}
If you allow this then it means that you can get a non-const pointer
through a const object, which breaks the transitivity of const.
More information about the Digitalmars-d
mailing list