[Issue 9528] New: std.array.appender can't append elements with const members

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Feb 17 17:37:09 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9528

           Summary: std.array.appender can't append elements with const
                    members
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: hsteoh at quickfur.ath.cx


--- Comment #0 from hsteoh at quickfur.ath.cx 2013-02-17 17:37:08 PST ---
Code:
------------------------------------------
import std.array;

E[] fastCopy(E)(E[] src) {
        auto app = appender!(const(E)[])();
        foreach (i, e; src)
                app.put(e);
        return app.data;
}

E[] slowCopy(E)(E[] src) {
        E[] result;
        foreach (i, e; src)
                result ~= e;
        return result;
}

void main() {
        class C {}
        struct S { const(C) c; }
        S[] s = [ S(new C) ];

        //auto t = fastCopy(s); // Does not compile
        auto t = slowCopy(s);
}
------------------------------------------

If fastCopy is used in place of slowCopy, dmd git head gives:
------------------------------------------
/usr/src/d/phobos/std/array.d(2256): Error: cannot modify struct
(cast(S*)(*this._data).arr)[len] S with immutable members
test.d(6): Error: template instance
std.array.Appender!(const(S)[]).Appender.put!(S) error instantiating
test.d(22):        instantiated from here: fastCopy!(S)
test.d(7): Error: cannot implicitly convert expression (app.data()) of type
const(S)[] to S[]
test.d(22): Error: template instance test.fastCopy!(S) error instantiating
------------------------------------------

Is there any workaround for this? What I'm trying to accomplish is to copy an
array of elements with const fields, but selectively skip elements based on
some predicate (so straight .dup is out of the question). But using =~ is slow
because of continual resizing/reallocation.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list