[Issue 15848] Identity opAssign not called on out parameters

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Jun 14 17:52:02 PDT 2016


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

Mathias Lang <mathias.lang at sociomantic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mathias.lang at sociomantic.co
                   |                            |m
           Hardware|x86_64                      |All
            Summary|out doesn't call opAssign() |Identity opAssign not
                   |                            |called on out parameters
                 OS|Linux                       |All

--- Comment #5 from Mathias Lang <mathias.lang at sociomantic.com> ---
Note: Your `opAssign` is not an identity `opAssign`
(http://dlang.org/spec/struct.html#assign-overload), so it wouldn't be called
in any copying situation.

Correct test code:

```
import std.stdio;

void foo(out Test x) {
    writeln("x.n = ", x.n);
}

struct Test {
    int n;
    ~this() {
        writeln("~this()");
    }
    ref Test opAssign(Test val) {
        writefln("opAssign(%s)", val);
        return this;
    }
}

void main() {
    Test t;
    foo(t);
    writeln("done");
}
```

And this doesn't call `opAssign` either (same output).

What should happen here:
- The destructor should be called if no `opAssign` is defined, because the
compiler provides an elaborate `opAssign` when it sees a struct with a dtor or
postblit being copied.
- If an identity `opAssign` is defined, it should be called.

I'll change the title, because `out` can be contract as well.

--


More information about the Digitalmars-d-bugs mailing list