Hacker News new | ask | show | jobs
by ginko 1748 days ago
In my company we do so by disallowing constructors to fail. If an object needs to be initialized with the potential of failure you need to call an init() function that returns an error code after construction.

Yes, that makes RAII basically impossible.

2 comments

IMO the most important part of RAII is really the other side, "destruction is resource divestment" - and that still works. That is the main purpose of lock guards, refcounting wrappers, file objects and so on.
Why not a factory function that returns std::optional<T> instead? Or better a variant of T or error code?

The construct-then-init pattern is so annoying to deal with.