[Issue 20881] [DIP1000] Templates seem to ignore 'return' (workaround)

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jun 7 19:27:27 UTC 2020


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

--- Comment #5 from ag0aep6g <ag0aep6g at gmail.com> ---
(In reply to Stanislav Blinov from comment #4)
> ? malloc it in a constructor, free it in a destructor, disable copy and
> assignment

Makes sense. When I think about DIP 1000, I tend to think of pointers to the
stack. But yeah, it's also supposed to enable malloc/free, isn't it.

For future readers, a full example:

----
import core.stdc.stdlib: free, malloc;
struct Faulty
{
    private int* ptr;
    this(int v) @trusted
    {
        ptr = cast(int*) malloc(int.sizeof);
        *ptr = v;
    }
    ~this() @trusted { if (ptr !is null) free(ptr); }
    @disable this(this);
    @disable void opAssign(Faulty);
    int* get1() @safe return { return ptr; }
    int* get2()() @safe return { return ptr; }
}
void main() @safe
{
    int* outlive;
    {
        auto f1 = Faulty(42);
        outlive = f1.get1(); /* error */
        outlive = f1.get2(); /* should be error */
    }
    /* `outlive` is invalid from here on */
}
----

--


More information about the Digitalmars-d-bugs mailing list