Hacker News new | ask | show | jobs
by codeflo 1482 days ago
Rust doesn’t have initializers in the C++ sense. And I mean, even in C++ many classes like shared_ptr or even lock_guard have constructors that don’t acquire the resources yet. When the resource is acquired is an API design concern; the important part is where it’s released.
1 comments

This sounds like an argument that C++ isn't RAII.
C++ _isn't_ RAII. RAII is a design pattern you can apply, leveraging C++ language features, in C++ in certain cases to avoid a certain class of bugs.
Well sure but I would assume the pattern would be consistently implied in the standard library, no?
The standard library necessarily has broad coverage of many use cases. That's why not every constructor of lock_guard acquires the lock. It's a completely legitimate use case that your thread happens to have a lock and wishes to use the end of lifetime of a lock_guard to release it. It's the same reason that you can construct a unique_ptr from an object that was allocated with new instead of with make_unique. Also, it's perfectly analogous to Go's `defer mu.Unlock()`.
I'm not an expert, but I would so no, it's not. The point of learning to implement with RAII, in the context of C++, is so that one will apply it to use C++ in a more resource safe way. It will help avoid the bugs that the language will inherently allow if the programmer is not being otherwise being careful.