[Issue 22498] auto ref function with auto ref parameter causes noncopyable payload be cleaned twice

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 9 19:37:52 UTC 2021


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

Tomáš Chaloupka <chalucha at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|critical                    |normal

--- Comment #1 from Tomáš Chaloupka <chalucha at gmail.com> ---
I've found possible workaround.

```
auto ref unwrap(EX)(auto ref EX res) {
    static if (__traits(isRef, res)) {
        printf("unwrap(ref)\n");
        return res.get();
    } else {
        printf("unwrap(val)\n");
        auto ret = res.get().move(); // < NRVO
        return ret;
    }
}
```

Then with `Foo f = gen(42).unwrap;`

It otputs:
```
~this(0)
~this(0)
~this(0)
unwrap(val)
~this(0)
~this(42)
```

But I'm still puzzled about what's wrong with the original one.

--


More information about the Digitalmars-d-bugs mailing list