how to copy const struct with indirections to mutable one (of the same type)

drug via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 29 02:46:04 PST 2016


I had the following code:
```
import std.algorithm: equal;

struct Data
{
	int[3] arr;
}

int main()
{
	auto const_data = const(Data)([1, 2, 3]);
	assert(const_data.arr[].equal([1, 2, 3]));
		
	Data mutable_data = const_data;
	assert(mutable_data.arr[].equal([1, 2, 3]));
		
	return 0;
}
```
It workred fine. Now I replace static array by std.container.Array:
```
import std.algorithm: copy, equal;
import std.container: Array;

struct Data
{
	Array!int arr;
	
	this(R)(R r)
	{
		r.copy(arr[]);
	}

     ref Data opAssign(const(Data) other)
     {
         pragma(msg, __FUNCTION__);

	import std.range: lockstep;

	this.arr.length = other.arr.length;
         foreach(ref s, ref d; lockstep(other.arr[], this.arr[]))
             d = s;

         return this;
     }
}

int main()
{
	auto const_data = const(Data)([1, 2, 3]);
	assert(const_data.arr[].equal([1, 2, 3]));
		
	Data mutable_data = const_data;
	assert(mutable_data.arr[].equal([1, 2, 3]));
		
	return 0;
}
```

and it fails now because:
``
Error: conversion error from const(Data) to Data
```
I can solve my problem in other ways but I'd like to know why opAssign 
overload doesn't work in this case?

Thank in advance


More information about the Digitalmars-d-learn mailing list