Skip to content

[QUESTION] Mutable that in move constructor? #715

Closed

Description

@SKyletoft

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.

Activity

  1. changed the title [-][QUESTION] [/-] [+][QUESTION] Mutable `that` in move constructor?[/+] on Oct 1, 2023
  2. jituderaps commented on Oct 1, 2023

    @jituderaps

    In cpp1, move constructor ( T (T&& that) ) only invoked when you construct an object of type T with an T's rvalue.
    i.e.

    Point p1 = Point(3, 4);
    Point p2 = std::move(p1);
    

    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) = {}.

  3. JohelEGP commented on Oct 1, 2023

    @JohelEGP
    Contributor
  4. SebastianTroy commented on Oct 2, 2023

    @SebastianTroy
  5. Repository owner locked and limited conversation to collaborators on Oct 8, 2023
  6. converted this issue into a discussion #735 on Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions