shallow copy of const(Object)[]
Douglas Petterson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Oct 31 16:27:55 PDT 2014
On Friday, 31 October 2014 at 18:39:00 UTC, anonymous wrote:
> I have a const(Object)[] and I want a shallow copy of the array.
> .dup doesn't do it, which I thought a bug, but according to
> Martin Nowak it's by design [1].
> std.array.array fails, too. Is there really nothing in phobos
> for
> this?
>
> static import std.array;
> void main()
> {
> const(Object)[] a;
>
> version(dup) auto b = a.dup;
> /* Nope. Apparently, dup is supposed to convert the
> elements
> to mutable [1],
> which doesn't work with const(Object), of course. */
>
> version(array) auto c = std.array.array(a);
> /* Nope. Tries to convert to mutable, too? */
>
> version(meh)
> {
> typeof(a) d;
> d.reserve(a.length);
> foreach(e; a) d ~= e;
> }
> }
>
> [1]
> https://github.com/D-Programming-Language/druntime/pull/1001#discussion_r19674927
In a such setup I'd rather define "b" as a pointer to the const
array "a", after all if "a" is const, a pointer to "a" would be
safe ? I mean its adress wont change. I also meant that if the
content doent change (const) there is no need to copy.
But that's a workaround, It's all about what const(type) mean
here.
My two kopeks...
More information about the Digitalmars-d-learn
mailing list