8 ms·
If not exception, then how to fail constructor?
by rossmohax 5y ago
If not exception, then how to fail constructor?
- CyberRabbi 5y agoThe “Herbceptions” proposal addressed this. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r1.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p070...
- synergy20 5y agoyou can still set a static flag or something in ctor then check it, but you can't do anything about dtor indeed if you need throw there.
- Koshkin 5y agoExcept you are not supposed to throw in a destructor.
- synergy20 5y agohttps://www.cs.technion.ac.il/users/yechiel/c++-faq/dtors-shouldnt-throw.html https://www.cs.technion.ac.il/users/yechiel/c++-faq/dtors-sh... but what if dtor fails(e.g. can't release a resource), is the by-default action terminate or ignore?
- Koshkin 5y agoWell, the "environment" may decide for you in some cases, but there is no default action as such. You log an error and either terminate or ignore - depending on which causes less harm.
- worik 5y agoIt has been many years since I used C++ But returning null is an obvious choice. Combined types which return the structure or an error are another. Has the advantage of returning error information, why failure. There are many ways
- roger10-4 5y agoYou can’t return null from a constructor (constructors have no return)
- worik 5y agoWhat dumb design. I thought the constructor returned a pointer to the memory created. No return? Stupid!
- gpderetta 5y agoFWIW, constructors do not "create" (I assume you mean allocate) memory. That's the job of operator new. A constructor, given a block of untyped memory, will construct an object in it.
- worik 5y agoRight. Been a long time. I would still expect a constructor to return. How can you tell if it failed?
- Calavar 5y ago> But returning null is an obvious choice. It's not because C++ is not an everything-is-a-reference language. > Combined types which return the structure or an error are another. Has the advantage of returning error information, why failure. Sum types are great, but changing constructors to return sum types would break all existing code. It would also lead to a whole slew of questions about how the construction of arrays of values would work. (Does an array of an objects now turn into an array of wrapper types? Byebye SIMD optimization!) It would also break the consistency of constructing POD vs non POD types, which would make writing templates that need to generalize over both a huge PITA.
- _0w8t 5y agoSignal failure via an out parameter reference. For destructors store the reference in the object and call an error method on errors.
- mannykannot 5y agoI think that would be problematical in those cases where the special constructors are called, and complicate using constructors in expressions and argument lists.
- ncmncm 5y agoThat is the worst of all possible choices. Just throw.
- _b 5y agoDon't write constructors that can fail unless it is failure that would be appropriate to crash for. That works out a lot better than it might naively sound. It is hard for people to reason about the possibility of constructors/destructors failing, so actually rather nice to just forbid it.
- addaon 5y agoAnd for those super-rare cases where it's appropriate to crash / fail hard in a constructor but you still have requirements about reporting or even recovery... setjmp/longjmp still exist.
- ncmncm 5y agoYou have always had the choice available to do something overwhelmingly worse than throwing. That is not a reason to do it.
- throwaway5486nv 5y ago