`defer` and `errdefer` give you 90% of what destructors & co give you for 0% of the complexity.
For now the the closest thing to RAII is this proposal, but there is no guarantee that it will be accepted.
For now the the closest thing to RAII is this proposal, but there is no guarantee that it will be accepted.
https://en.cppreference.com/w/cpp/language/rule_of_three
C++ RAII also rubs up painfully against handle based APIs (looking at you Windows) in my experience. There is a lack of standardized RAII wrappers for handle types like there is for pointers. Yes you can pull in a custom handle RAII wrapper but at that point it's simpler to just manage manually.
Defer on the other hand is simple. Anyone can understand it in five minutes.
.. what's wrong with
#include <memory>
template<typename T, auto Free>
using safe_handle_t = std::unique_ptr<T, decltype([] (auto p) { Free(p); })>;
using file_handle = safe_handle_t<FILE, fclose>;
void file_example() {
file_handle f{fopen("foo", "r")};
} auto f = fopen("foo", "r");
if(f)
fclose(f);
check for yourself: https://gcc.godbolt.org/z/YxaPqYGez