You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
in that makes sense because it is a copy and you aren't modifying that.
move that makes sense because that is an R value reference, and you can steal it's resources with impunity, knowing that nothing else can mistakenly use those resources as it will go out of scope and destruct after use.
inout that would be dangerous, like you say, you become in charge of setting moved resources to sensible null values e.t.c otherwise something else might later also assume ownership and modify them without the first owners knowledge or consent. This option is error prone and probably not what you wanted to do anyway?
On 1 October 2023 20:29:27 Samuel Kyletoft ***@***.***> wrote:
How on earth do I write a move constructor that resets the moved from object? (Like setting pointers to null or the like).
Going from this:
struct Bar {
int b = 5;
Bar(int b_): b(b_) {}
Bar(Bar& that) {
this->b = that.b;
that.b = 0;
}
};
I tried to write this:
Foo: @struct type = {
public a: i32 = 5;
operator=: (out this, a_: i32) = {
a = a_;
}
operator=: (out this, inout that) = {
this.a = that.a + 1;
that.a = 0;
}
}
But I'm only allowed to use in or move for that? Surely this is not just unsupported?
Sorry to take up space in the issue tracker, but I'm not aware of any better forum.
—
Reply to this email directly, view it on GitHub<#715>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AALUZQPAIXR3RKATTCJ7C6DX5HABJANCNFSM6AAAAAA5ORA2O4>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
Activity
[-][QUESTION] [/-][+][QUESTION] Mutable `that` in move constructor?[/+]jituderaps commentedon Oct 1, 2023
In cpp1, move constructor (
T (T&& that)) only invoked when you construct an object of type T with an T's rvalue.i.e.
in above case, move constructor(
Point(Point&& that)) get invoked to construct variable p2.so,
Bar (Bar& that)is not a syntax of move constructor.In cpp2, the syntax for move constructor is :
operator=:(out this, move that) = {};.And if you still want to do same thing as
Bar (Bar& arg)in cpp2, then you can do that as given below:operator=: (out this, inout rhs: Bar) = {}.JohelEGP commentedon Oct 1, 2023
https://github.com/hsutter/cppfront/discussions/categories/q-a is a place for questions.
SebastianTroy commentedon Oct 2, 2023