15
Modern C++ — RAII (green7ea.github.io)
submitted 2 days ago by [email protected] to c/[email protected]
top 6 comments
sorted by: hot top new old
[-] [email protected] 3 points 13 hours ago* (last edited 13 hours ago)

This is 14 years old by now - hardly "modern". RAII itself is even older.

[-] [email protected] 1 points 6 hours ago

100% agree. But I like posting articles like these because it brings me back to how I learned programming, and Linux specifically - namely by reading a bunch of articles from similar link aggregators and sharing sites.

My hope is that sharing articles like these is a form of planting the seeds for another cycle for people to learn the way that I did.

[-] [email protected] 3 points 1 day ago* (last edited 1 day ago)

What I find interesting is that move semantics silently add something to C++ that did not exist before: invalid objects.

Before, if you created an object, you could design it so that it kept all invariants until it was destroyed. I'd even argue that it is the true core of OOP that you get data structures with guaranteed invariants - a vector or hash map or binary heap never ceases to guarantee its invariants.

But now, you can construct complex objects and then move their data away with std::move() .

What happens with the invariants of these objects?

[-] [email protected] 2 points 1 day ago

Depends on the object what happens when they are moved from. Some objects are put into a valid moved from state (usually depends on if doing so is free or required anyway. For example to satisfy the invariant of the moved to unique pointer the moved from pointer needs to be set to nullptr in order to prevent the moved tos unique pointer being deleted from underneath it)

[-] [email protected] 1 points 15 hours ago

So what do you do with a file object?

[-] [email protected] 1 points 11 hours ago

Douno off the top of my head. To take a wild guess they might just wrap a file handle and give it s nice api? If that's what they do then moving from the file zeros out he handle for basically the same reason smart pointers set their internal pointer to nullptr, so they don't delete it (or close the file in this case) underneath the new object.

this post was submitted on 30 May 2025
15 points (94.1% liked)

Programming

20436 readers
198 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS