www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.container.Array deep-copy?

reply "qznc" <qznc web.de> writes:
How can you deep-copy a std.container.Array instance?

The actual array data is heap-allocated and reference-counted.
Assignment and .dup only create additional references.

Using a copy constructor yields an error:

Array!Foo x;
Array!Foo y = Array!Foo(x);

Error: template std.container.Array!(Foo).Array.__ctor cannot
deduce function from argument types !()(Array!(Foo)), candidates
are:
/opt/compilers/dmd2/include/std/container.d(2652):
std.container.Array!(Foo).Array.__ctor(U)(U[] values...) if
(isImplicitlyConvertible!(U, T))
/opt/compilers/dmd2/include/std/container.d(2670):
std.container.Array!(Foo).Array.__ctor(Stuff)(Stuff stuff) if
(isInputRange!Stuff &&
isImplicitlyConvertible!(ElementType!Stuff, T) && !is(Stuff ==
T[]))


The question came up in a reddit discussion:
http://www.reddit.com/r/programming/comments/2ipdpa/floss_weekly_311_the_d_language/cl4yv8w
Oct 09 2014
parent reply "qznc" <qznc web.de> writes:
On Thursday, 9 October 2014 at 21:14:46 UTC, qznc wrote:
 How can you deep-copy a std.container.Array instance?
Ok, the deep-copy problem already got resolved on reddit: Use dup. However, the error is still open. You cannot give an Array!X argument to constructor/replace/insertBefore of Array!X instances?
Oct 09 2014
parent reply "yazd" <yazan.dabain gmail.com> writes:
On Thursday, 9 October 2014 at 21:24:55 UTC, qznc wrote:
 On Thursday, 9 October 2014 at 21:14:46 UTC, qznc wrote:
 How can you deep-copy a std.container.Array instance?
Ok, the deep-copy problem already got resolved on reddit: Use dup. However, the error is still open. You cannot give an Array!X argument to constructor/replace/insertBefore of Array!X instances?
You will just need to slice it to provide a range.
Oct 09 2014
parent reply "qznc" <qznc web.de> writes:
On Friday, 10 October 2014 at 06:27:35 UTC, yazd wrote:
 On Thursday, 9 October 2014 at 21:24:55 UTC, qznc wrote:
 On Thursday, 9 October 2014 at 21:14:46 UTC, qznc wrote:
 How can you deep-copy a std.container.Array instance?
Ok, the deep-copy problem already got resolved on reddit: Use dup. However, the error is still open. You cannot give an Array!X argument to constructor/replace/insertBefore of Array!X instances?
You will just need to slice it to provide a range.
Like the following? That did not work. Array!Foo y = Array!Foo(x[]);
Oct 10 2014
parent reply "yazd" <yazan.dabain gmail.com> writes:
 Like the following? That did not work.

 Array!Foo y = Array!Foo(x[]);
How does it not work? It compiles successfully: http://dpaste.dzfl.pl/583d20e426a0
Oct 10 2014
parent reply "Sag Academy" <sagacademyjaipur gmail.com> writes:
On Friday, 10 October 2014 at 10:32:17 UTC, yazd wrote:
 Like the following? That did not work.

 Array!Foo y = Array!Foo(x[]);
How does it not work? It compiles successfully: http://dpaste.dzfl.pl/583d20e426a0
yeah man.
Oct 10 2014
parent "qznc" <qznc web.de> writes:
On Friday, 10 October 2014 at 10:59:59 UTC, Sag Academy wrote:
 On Friday, 10 October 2014 at 10:32:17 UTC, yazd wrote:
 Like the following? That did not work.

 Array!Foo y = Array!Foo(x[]);
How does it not work? It compiles successfully: http://dpaste.dzfl.pl/583d20e426a0
yeah man.
You are right. Sorry. I probably messed up my test file somehow.
Oct 10 2014