Array of const objects with indirections and std.algorithm.copy

drug via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 27 04:51:01 PDT 2016


I have the following:

```
struct Foo
{
	int[] i;
	
	this(int[] i)
	{
		this.i = i.dup;
	}
	
	ref Foo opAssign(ref const(this) other)
	{
		i = other.i.dup;
		
		return this;
	}
}

const(Foo)[] cfoo;
```

I need to copy it:

```
Foo[] foo;

cfoo.copy(foo); // fails to compile because phobos in case of array uses
                 // array specialization and this specialization fails
                 // see 
https://github.com/dlang/phobos/blob/v2.071.1/std/algorithm/mutation.d#L333
```

but it works:
```
foreach(ref src, ref dest; lockstep(cfoo, foo))
		dest = src;
```

In my opinion the possible decision is disabling the array 
specialization if the arrays can't be low-level copied. And the question 
raises here - why `areCopyCompatibleArrays` doesn't work in this case? I 
found that
```
	// return true
	pragma(msg, __traits(compiles, { typeof(foo).init[] = 
typeof(cfoo).init[]; } ))
	// return false
	pragma(msg, __traits(compiles, { foo[] = cfoo[]; } ))
```
The question is - shouldn't `areCopyCompatibleArrays` be modified 
according code above to use array specialization where it is appropriate 
and allow to copy arrays by elements when it is needed?

The full code is here https://dpaste.dzfl.pl/5e13e183a006


More information about the Digitalmars-d-learn mailing list