insertInPlace differences between compilers
John McFarlane via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Nov 11 12:53:49 PST 2014
I'm trying to write a struct template that uses `insertInPlace`.
However, it doesn't work with certain template type / compiler
combinations. Consider the following:
import std.range;
struct S { const int c; }
S[] a;
insertInPlace(a, 0, S());
With DMD64 D Compiler v2.066.1, I get the following error:
/usr/include/dmd/phobos/std/array.d(1013): Error: cannot modify
struct dest[i] S with immutable members
/usr/include/dmd/phobos/std/array.d(1079): Error: template
instance std.array.copyBackwards!(S) error instantiating
./d/my_source_file.d(12345): instantiated from here:
insertInPlace!(S, S)
But with gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1), the same
code compiles.
While I'm still trying to get to grips with mutability in D, I
wonder which compiler/library's got it right or whether this is a
difference in language versions. The error still occurs when I
change the type of `c` to a const class which is the case I'd
really like this to work for.
I can avoid `insertInPlace` on DMD with the following, much
slower alternative:
a = a[0..insertionPoint] ~ element ~ a[insertionPoint..$];
In the short term, could anybody suggest a `static if` expression
to determine whether I can copy the type to the satisfaction of
`copyBackwards`? I tried isMutable but that didn't seem to work.
Thanks, John
More information about the Digitalmars-d-learn
mailing list