advise about safety of using move in an opAssign with __traits(isRef
Paul Backus
snarwin at gmail.com
Fri Aug 16 14:29:26 UTC 2019
On Friday, 16 August 2019 at 08:07:28 UTC, aliak wrote:
> Hi, I'm trying to fix a use-case where you have a wrapper
> template type (it's an optional) and the wrapping type has
> @disable this(this). And having this scenario work:
>
> [...]
>
> 2) use isRef inside opAssign like this:
>
> void opAssign(auto ref Optional!T lhs) {
> static if (__traits(isRef, lhs)) {
> this._value = lhs._value;
> } else {
> import std.algorithm: move;
> move(lhs._value, this._value);
> }
> }
>
> Then you could just do:
>
> field = fun
>
> And it'll just work since fun returns an rvalue. The second
> solution it seems like it'll just work correctly with the
> static if guard. But I'm not sure if there're any gotchas I
> should be aware of?
>
> Any advice?
Even simpler:
void opAssign(auto ref Optional!T rhs) {
import core.lifetime: forward;
this._value = forward!rhs;
}
More information about the Digitalmars-d-learn
mailing list