On 12/13/18 8:17 AM, aliak wrote:
On Thursday, 13 December 2018 at 12:08:22 UTC, Boris-Barboris wrote:
On Thursday, 13 December 2018 at 09:51:42 UTC, aliak wrote:
[...]


You can just move in container constructor:


struct S {
    @disable this(this);
    this(int i) {}
}

struct Container(T) {
    T value;
    this(T value) {
        import std.algorithm: move;
        this.value = value.move;
    }
}

void main() {
    auto a = Container!S(S(3));
}

Ah. Is there any case where you would not want to do that when you have a T value as parameter?

Probably not an issue. Note that if you take the parameter by value, the T must be an rvalue if it has disabled postblit.

In other words, this wouldn't work:

S s;
auto a = Container!S(s); // error

So moving it isn't going to affect anything outside the function.


And, what if it's "this()(auto ref T value)"? Then moving could be dangerous if the parameter was passed as a ref. Or maybe it just wouldn't compile?

What will happen is then you *could* take an already existing T as a parameter, and the T you pass in will be destroyed upon calling the constructor.

move resets the original to the .init value if it has a destructor or postblit.

Is it dangerous? Probably not, but you may want to document for anyone who uses the container to expect that.

-Steve

Reply via email to