Is the Rule of 5 (for constructors and destructors) outdated, if smart pointers can take care of resource management?

The full name of the rule is the rule of 3/5/0. It doesn’t say “Always provide all five”. It says that you have to either provide the three, the five, or none of them. Indeed, more often than not the smartest move is to not provide any of the five. But you can’t do that … Read more

Can Tail Call Optimization and RAII Co-Exist?

Taken at face-value, it would certainly seem like RAII works against TCO. However, remember that there are a number of ways in which the compiler can “get away with it”, so to speak. The first and most obvious case is if the destructor is trivial, meaning that it is the default destructor (compiler-generated) and all … Read more

How to handle constructor failure for RAII

The problem is that your class is trying to do too much. The principle of RAII is that it acquires a resource (either in the constructor, or later), and the destructor releases it; the class exists solely to manage that resource. In your case, anything other than DoSomething() and UndoSomething() should be the responsibility of … Read more

Does ScopeGuard use really lead to better code?

It definitely improves your code. Your tentatively formulated claim, that it’s obscure and that code would merit from a catch block is simply not true in C++ because RAII is an established idiom. Resource handling in C++ is done by resource acquisition and garbage collection is done by implicit destructor calls. On the other hand, … Read more

Do programmers of other languages, besides C++, use, know or understand RAII?

There are plenty of reasons why RAII isn’t better known. First, the name isn’t particularly obvious. If I didn’t already know what RAII was, I’d certainly never guess it from the name. (Resource acquisition is initialization? What does that have to do with the destructor or cleanup, which is what really characterizes RAII?) Another is … Read more

RAII in Python – automatic destruction when leaving a scope

tl;dr RAII is not possible, you mix it up with scoping in general and when you miss those extra scopes you’re probably writing bad code. Perhaps I don’t get your question(s), or you don’t get some very essential things about Python… First off, deterministic object destruction tied to scope is impossible in a garbage collected … Read more

RAII tutorial for C++ [closed]

There’s nothing to it (that is, I don’t think you need a full tutorial). RAII can be shortly explained as “Every resource requiring cleanup should be given to an object’s constructor.” In other words: Pointers should be encapsulated in smart pointer classes (see std::auto_ptr, boost::shared_ptr and boost::scoped_ptr for examples). Handles requiring cleanup should be encapsulated … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)