Hacker News new | ask | show | jobs
by somedudetbh 1482 days ago
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.
1 comments

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.