7 ms·
Do you do RAII? It effectively forces exceptions, because nontrivial initialization necessarily means the possibility of failure, but exceptions are the only me
by lmm 10y ago
Do you do RAII? It effectively forces exceptions, because nontrivial initialization necessarily means the possibility of failure, but exceptions are the only mechanism available for constructors to report failure (well, except for even worse things like an errno-like global variable).
- cmrdporcupine 10y agoI've gotten in the habit of doing "RAII"-like as: - basic non-throwing initialization only constructor - a "bool Initialize" method which does what the constructor would have done, but returns false on fail - destructor as usual to clean up But this is because I'm at Google where exceptions-are-no.
- realharo 10y agoSo how do you protect yourself against inadvertently passing around uninitialized objects?
- cmrdporcupine 10y agoCautioun. And the destructor has to check for initialization before cleanup.
- ryandrake 10y agoAm I the only one that finds it ironic that you have a typo in the word "Caution"? I'd be very suspicious of any failure avoidance system that relies on "user caution".
- SamReidHughes 10y agoYou'll get a run time error.
- lmm 10y agoMaybe. Or maybe you'll get an invisible resource leak, or silent data corruption, or an RCE vulnerability.
- SamReidHughes 10y agoWell I don't know about that. If you have a datatype whose constructors can't throw, it should still assert at run time about its state that it's been initialized. Like, you have to actually do that, of course. So there's more surface area for mistakes inside the object, but from the outside of the object, which is what I think this sub-thread is talking about, they get caught at run time.
- lmm 10y agoIt shouldn't have to be assertions though - you should make it correct by, well, construction. When the fields are RAII the constructor can just initialize them. If your class has different possible states and flags it's usually correct to promote the different states to differnt types (subclasses).
- SamReidHughes 10y agoI don't think we're disagreeing about anything. The scope of my comment is in reference to "So how do you protect yourself against inadvertently passing around uninitialized objects?" The answer is, with a run-time error.
- AnimalMuppet 10y agoBut all failure avoidance systems rely on user caution. Error return values have to be checked. Exceptions have to be caught and handled (correctly). RAII has to be done everywhere a resource is allocated. Now, true, some of these impose lesser burdens than others. Arguably, RAII imposes the least - but not zero. And if you forget with any resource, for that resource you are vulnerable.
- lmm 10y agoThat we can't make things perfect is no reason not to make things better. And ultimately it should be possible to enforce that all resources are managed, because ultimately all resource interfaces have to be provided by the language runtime. To a certain extent enforcing user-defined invariants is always a matter of taste, but at the same time a language that makes it easy makes people a lot better at it.
- gpderetta 10y agoExactly. Objects with an artificial invalid state are an antipattern in idiomatic c++.
- brandmeyer 10y agoGOOG is a big company, and not everyone handles this the same way. At least some teams are doing what the sibling mentioned: A static factory-like method that returns a discriminated union of either T or an error type. It is conceptually similar to std::variant<T, std::error_condition>, but syntactically optimized for this use case.
- praptak 10y agoGoogle has StatusOr<T> for this purpose: https://github.com/google/protobuf/blob/master/src/google/protobuf/stubs/statusor.h https://github.com/google/protobuf/blob/master/src/google/pr...
- brandmeyer 10y agoYep, that's the one.
- deleted 10y ago[deleted]
- user5994461 10y agoI tried to imagine the error message given by this template when something is written wrong. Just had a vision of horror.
- praptak 10y agoWhy global? Just check if the resource is properly initialized: std::ofstream file("example.txt"); if (!file.is_open()) return; // ofstream will close in destructor. And RAII doesn't even imply that the acquisition must happen via constructor. Edit: Technically it does, but the above pattern would not change if 'file' was a result of calling a free function or whatever.
- lmm 10y ago> Just check if the resource is properly initialized: std::ofstream file("example.txt"); if (!file.is_open()) return; This is exactly not RAII. You acquired the resource and it might not be initialized. And it has the same problem: it's very easy to pass file around when it's not actually valid.
- praptak 10y agoYes, this forces you to explicitly check for errors. I find uninitialized objects' badness comparable to the badness of unhandled exceptions - neither is very hard to debug. But full exception safety is obviously much more than just properly catching exceptions. I mean avoiding subtle traps like the ones here: https://herbsutter.com/gotw/_102/ https://herbsutter.com/gotw/_102/
- realharo 10y ago>I find uninitialized objects' badness comparable to the badness of unhandled exceptions - neither is very hard to debug. Unless you have a bunch of code that just passes the object around without looking into it (so it won't fail there), and then when the crash finally happens, you have no idea where the invalid object came from. This is really just another form of the billion dollar mistake, except you have invalid objects instead of null pointers (or in the worst case, you have both). With an unhandled exception, you always have the context of where and why it happened (assuming you know the reason behind throwing it).
- Aeolos 10y agoThis code suffers from a race condition: the file might be deleted/moved/renamed at any point, even if the file.is_open() check succeeds. Code that does not consider this possibility is technically fragile under a wide set of pretty common conditions.
- realharo 10y agoI guess another option would be a private constructor + a static factory method that returns an optional.
- user5994461 10y ago> but exceptions are the only mechanism available for constructors to report failure use abort() from stdlib.h
- lmm 10y agoThat's effectively an alternative implementation of exceptions.