Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions workshop/04-ownership/slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,18 @@ In C++ we have move semantics, in Rust there is semantical move - moved object i
### Move in C++

```cpp
float area(circle: Circle) {
return std::numbers::pi_v<float> * circle.radius * circle.radius;
// `circle` destructor is called
float area(Circle&& circle) {
Circle local = std::move(circle);
return std::numbers::pi_v<float> * local.radius * local.radius;
// `local` destructor is called
}

int main() {
auto circle = Circle(2.0);
// `circle` moved to `area` - move constructor is called, which leaves
// old `circle` in undefined destructible state
// `circle` moved to `area` - move constructor may be called, which leaves
// old `circle` in after-move state
std::cout << "Circle area: " << area(std::move(circle)) << '\n';
// Circle is a valid but undefined object here - basically semantic UB.
// Circle may be a valid but unspecified object here - basically semantic UB.
std::cout << "Circle area: " << area(std::move(circle)) << '\n';
// `circle` destructor is called
}
Expand Down