Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Mutable that in move constructor? #715

Closed
SKyletoft opened this issue Oct 1, 2023 · 3 comments
Closed

[QUESTION] Mutable that in move constructor? #715

SKyletoft opened this issue Oct 1, 2023 · 3 comments

Comments

@SKyletoft
Copy link

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.

@SKyletoft SKyletoft changed the title [QUESTION] [QUESTION] Mutable that in move constructor? Oct 1, 2023
@jituderaps
Copy link

jituderaps commented 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.

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

@JohelEGP
Copy link
Contributor

JohelEGP commented Oct 1, 2023

https://github.com/hsutter/cppfront/discussions/categories/q-a is a place for questions.

@SebastianTroy
Copy link

SebastianTroy commented Oct 2, 2023 via email

Repository owner locked and limited conversation to collaborators Oct 8, 2023
@hsutter hsutter converted this issue into discussion #735 Oct 8, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

4 participants