5 ms·
> Let’s say I want to know if a constructor failed This seems like the beginning of a bad situation. I mean, this shows why constructors that do any complex wo
by henning 5y ago
> Let’s say I want to know if a constructor failed
This seems like the beginning of a bad situation. I mean, this shows why constructors that do any complex work that can error out are a bad idea.
- AnimalMuppet 5y agoNo, they're fundamental to how C++ is idiomatically used. "Resource acquisition is initialization". This means that, if you have an object, and creating that object can fail, then it should fail in the constructor. So you either have a valid object, or none at all - you never have an invalid object. Now, you could squint and say that an optional<T> returned from a factory is a valid object; that is, it's a valid optional<T>. I can sort of twist my mind that far, but it's not idiomatic. (It might become so, I suppose...)
- qayxc 5y agoHow is a factory function not idiomatic? It's not as if C++ is a single paradigm language that encourages a singular style or paradigm like OO. RAII is a mess in certain circumstances (looking at you, Vulkan API) and sometimes straight up just doesn't work anyway. If your initialisation is prone to failure, don't do it in a ctor.
- henning 5y agoThis just shows what a bad idea RAII is. You're destroying opportunities for managing resources in bulk and coupling that to just gathering a group of related data under a single identifier.
- AnimalMuppet 5y agoIn what possible way can RAII be said to do those things?