Can a struct be copied by its const method?

Ivan Kazmenko gassa at mail.ru
Fri Aug 2 08:06:33 PDT 2013


The question is as stated: can a struct be copied by its const 
method?  I'd expect the answer to be the same for any struct, but 
the following two examples say different.  I'm using DMD 2.063.2 
on Windows.

Example 1. Struct containing an int.  Postblit added to resemble 
Example 2 closely.
-----
int f (int val) {return val;}
struct Struct
{
	int val;
	this (this) {val = f (val);}
	void make_copy () const {dest = this;}
}
Struct dest;
void main () { }
-----

Example 2. Struct containing a dynamic array.
-----
struct Struct
{
	int [] arr;
	this (this) {arr = arr.dup;}
	void make_copy () const {dest = this;}
}
Struct dest;
void main () { }
-----

Example 1 compiles fine, but Example 2 gives an error:
arrs.d(12): Error: function arrs.Struct.opAssign (Struct p) is 
not callable using argument types (const(Struct))

Is that forbidden on purpose?  If so, why?  Removing the postblit 
does not help (and breaks the intent to make a full copy).

Workaround 1 is to make a cast:
	void make_copy () const {dest = cast (Struct) this;}

Workaround 2 is to remove const-ness:
	void make_copy () {dest = this;}

In my larger program, Workaround 1 leads to errors, while 
Workaround 2 works as intended.  However, I don't yet have a 
minimal example of that, so that may be my own bug.  Anyway, am I 
doing the cast right?

Ivan Kazmenko.


More information about the Digitalmars-d-learn mailing list