News for hackers. Stuff that matters.
Re:Current hardware trends make C++ exceptions harder to jus... by rossmohax on Tuesday February 22, 2022 @01:25PM (#30431478)
If not exception, then how to fail constructor?
Re:Current hardware trends make C++ exceptions harder to jus... by _b on Tuesday February 22, 2022 @01:57PM (#30431982)
Don'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.
Re:Current hardware trends make C++ exceptions harder to jus... by addaon on Tuesday February 22, 2022 @02:02PM (#30432073)
And 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.
Re:Current hardware trends make C++ exceptions harder to jus... by ncmncm on Tuesday February 22, 2022 @02:43PM (#30432639)
You have always had the choice available to do something overwhelmingly worse than throwing. That is not a reason to do it.
Re:Current hardware trends make C++ exceptions harder to jus... by fpoling on Tuesday February 22, 2022 @01:40PM (#30431715)
Signal failure via an out parameter reference. For destructors store the reference in the object and call an error method on errors.
Re:Current hardware trends make C++ exceptions harder to jus... by mannykannot on Tuesday February 22, 2022 @02:08PM (#30432162)
I think that would be problematical in those cases where the special constructors are called, and complicate using constructors in expressions and argument lists.
Re:Current hardware trends make C++ exceptions harder to jus... by ncmncm on Tuesday February 22, 2022 @02:40PM (#30432599)
That is the worst of all possible choices.

Just throw.

Re:Current hardware trends make C++ exceptions harder to jus... by CyberRabbi on Tuesday February 22, 2022 @01:28PM (#30431529)
The “Herbceptions” proposal addressed this. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p070...
Re:Current hardware trends make C++ exceptions harder to jus... by synergy20 on Tuesday February 22, 2022 @01:31PM (#30431590)
you 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.
Re:Current hardware trends make C++ exceptions harder to jus... by Koshkin on Tuesday February 22, 2022 @04:25PM (#30434051)
Except you are not supposed to throw in a destructor.
Re:Current hardware trends make C++ exceptions harder to jus... by synergy20 on Tuesday February 22, 2022 @06:45PM (#30435494)
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?
Re:Current hardware trends make C++ exceptions harder to jus... by Koshkin on Tuesday February 22, 2022 @07:27PM (#30435908)
Well, 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.
Re:Current hardware trends make C++ exceptions harder to jus... by worik on Tuesday February 22, 2022 @01:36PM (#30431660)
It 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

Re:Current hardware trends make C++ exceptions harder to jus... by Calavar on Tuesday February 22, 2022 @01:45PM (#30431792)
> 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.

Re:Current hardware trends make C++ exceptions harder to jus... by roger10-4 on Tuesday February 22, 2022 @01:42PM (#30431749)
You can’t return null from a constructor (constructors have no return)
Re:Current hardware trends make C++ exceptions harder to jus... by worik on Tuesday February 22, 2022 @11:34PM (#30437672)
What dumb design.

I thought the constructor returned a pointer to the memory created.

No return? Stupid!

Re:Current hardware trends make C++ exceptions harder to jus... by gpderetta on Wednesday February 23, 2022 @08:01AM (#30440489)
FWIW, 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.
Re:Current hardware trends make C++ exceptions harder to jus... by worik on Wednesday February 23, 2022 @02:36PM (#30445280)
Right.

Been a long time.

I would still expect a constructor to return. How can you tell if it failed?