[Issue 23556] Array append ignores copy constructor

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Dec 16 17:12:25 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=23556

Teodor Dutu <teodor.dutu at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |teodor.dutu at gmail.com

--- Comment #2 from Teodor Dutu <teodor.dutu at gmail.com> ---
a ~= S() is not supposed to call either the copy constructor or the postblit
because the newly appended element is supposed to be created in-place inside
the array. This test ensures this behaviour (only the constructor is called,
without the postblit):
https://github.com/dlang/dmd/blob/d405b343e301e5c37a90e66131b3f4d588bed2f2/compiler/test/runnable/sdtor.d#L2244-L2245

The issue comes from copying the array during reallocation. The postblit is
correctly called while the cpctor is not. The following code prints "loop" 15
times before printing "pblit" 15 times when the array is reallocated:

---
struct S
{
    this(this)
    {
        writeln("pblit");
    }
}

void main()
{
    S[] a = [ S() ];
    auto p = a.ptr;
    // append until reallocation
    while (a.ptr == p)
    {
        writeln("loop");
        a ~= S(); // no assert
    }
}
---

--


More information about the Digitalmars-d-bugs mailing list