News for hackers. Stuff that matters.
Re:Misra C++:2023 Published by captainmuon on Sunday December 17, 2023 @02:00PM (#38675338)
Exceptions are not non-deterministic, although the control flow can be a bit non obvious of course.

I think if you disallow exceptions you run into other problems. How can a constructor fail now? You need to have some kind of flag showing if an object is fully constructed. But then that goes against the idea to "make illegal states unrepresentable".

I do wish C++ had more tools to reign in exceptions. Maybe an "onlythrows X" annotation that says only these very specific exceptions may escape from a block, and the checker will complain if it cannot prove that only X can be thrown. The opposite of checked exceptions basically.

Re:Misra C++:2023 Published by jandrewrogers on Sunday December 17, 2023 @03:04PM (#38675888)
Handling constructor failure is one of the least valuable use cases for exceptions. Idioms for exception-free construction are straightforward and some cases will require these idioms even with exceptions.

Resource exhaustion or hardware failures are the more straightforward use cases for exceptions, but doing anything clever in those cases requires writing similar handling code as you would without exceptions.

Re:Misra C++:2023 Published by oytis on Sunday December 17, 2023 @04:33PM (#38676671)
In the domain where Misra is applied, resource exhaustion and hardware failures are totally valid scenarios that need to be processed in the same way as any other error.
Re:Misra C++:2023 Published by mgaunard on Sunday December 17, 2023 @05:10PM (#38676981)
It's actually the most useful one. It enables the creation of state invariants.
Re:Misra C++:2023 Published by GrumpySloth on Monday December 18, 2023 @08:17AM (#38681989)
Maintaining state invariants is trivial without exceptions thrown from constructors. Just make constructors private and write a public static factory method. Allowing exceptions in constructors on the other hand creates the problem of: what should the destructor of an only partially constructed object do? It’s just unnecessary.
Re:Misra C++:2023 Published by jstimpfle on Monday December 18, 2023 @10:07AM (#38683447)
I think there is a misconception on your part. Formally, there is no "half-constructed" object, at least not in any way that's missing an automatic mechanism to unwind mid-way (i.e. half-deconstruct).

Each object construction is a list of N + 1 construction stages -- constructing the N subobjects (implicitly or as per the initializer list), followed by the constructor body.

The destructor has N + 1 stages too, those match exactly the constructor's stages. If there is an exception happening in any stage of the constructor, say stage E, naturally only stages 0 to E-1 get un-done (in reverse), but not E nor any later stage.

So what you have to do is imagine that all sub-objects are already constructed, and imagine there is a function that runs the constructor body and destructor body in a sequence. The constructor part could throw an exception, causing the destructor part to never run. Like any other function, it should be possible to run it without leaking anything if an exception happens. Make it "exception safe" using RAII or by being extra careful.

If you've written constructor and destructor such that they match in this way (which is, again, like you would write any other function), then will work correctly in all cases. This is a powerful concept and pretty much fool-proof. I say that as someone who has lots of concerns about the language's complexity -- including exceptions.

Re:Misra C++:2023 Published by GrumpySloth on Monday December 18, 2023 @02:11PM (#38686826)
If you call mmap/VirtualAlloc/open/fopen in your constructor and later it throws, you will have a resource leak, because the destructor won’t clean it up.
Re:Misra C++:2023 Published by jstimpfle on Monday December 18, 2023 @07:52PM (#38690447)
Again, you need to make the constructor exception safe, that's just like any other function. Just imagine the constructor and destructor bodies as one combined function. (Of course don't forget the method calls in between but those should preserve the class invariant).
Re:Misra C++:2023 Published by mgaunard on Monday December 18, 2023 @10:07AM (#38683439)
The problem you mention doesn't exist, since destructors are not called when a constructor throws.
Re:Misra C++:2023 Published by GrumpySloth on Monday December 18, 2023 @02:12PM (#38686835)
If you call mmap/VirtualAlloc/open/fopen in your constructor and later it throws, you will have a resource leak, because the destructor won’t clean it up.
Re:Misra C++:2023 Published by mgaunard on Monday December 18, 2023 @06:20PM (#38689772)
If you don't know how to write exception-safe code -- for which RAII is the most common idiom -- then you have exception safety issues.

I don't see how that relates to the thread, other than to say exceptions in constructors are quite fundamental.

Re:Misra C++:2023 Published by jupp0r on Sunday December 17, 2023 @02:13PM (#38675451)
The constructor error problem is easily solved by using factory functions and two phase construction. The problem is that the standard library is relying on exceptions quite a bit and major parts become unusable.
Re:Misra C++:2023 Published by lelanthran on Sunday December 17, 2023 @02:29PM (#38675607)
For what definition of "easy"?

Simply passing by value can result in a constructor that fails, while still bypassing any of the factory functions.

Re:Misra C++:2023 Published by jupp0r on Sunday December 17, 2023 @03:45PM (#38676226)
Easy as in "competent C++ programmers do this every day"?

If the copy constructor can fail and you don't want that, then delete it?

Re:Misra C++:2023 Published by lelanthran on Monday December 18, 2023 @08:47AM (#38682380)
> If the copy constructor can fail and you don't want that, then delete it?

You're trivialising just how deeply embedded exceptions are into the design of the language.

I gave just one example and it was not meant to be exhaustive, just one 'gotcha' that you won't find out till runtime and your program starts (worst case) giving you slightly incorrect results without you knowing about it...

So, yeah, if you want to do without exceptions (without having your program execute random code) you need to know in advance what special cases to handle, like unintended copy construction, or failures in overloaded operators, or which std libs can be used and which cannot, or which C++ libraries can be linked, and which cannot.

All of which is perfectly possible, but taken together is hardly "easy". It's tedious, error-prone, bloated ... but hardly what someone would call "easy".

Re:Misra C++:2023 Published by jupp0r on Monday December 18, 2023 @09:17AM (#38682831)
C++ is complicated, I get it. Things that are "easy" in other languages are "hard" in C++. That doesn't mean that writing C++ code that can't throw isn't something that tens of thousands of engineers are doing every day. One could argue that all of C++ is tedious, bloated and error-prone.
Re:Misra C++:2023 Published by kwant_kiddo on Sunday December 17, 2023 @04:01PM (#38676375)
it is easy? It is the named constructor idiom.

I don't understand why jupp0r is getting downvoted?

Most big public c++ projects turn off exceptions, so it seems to be the norm more than anything.

Re:Misra C++:2023 Published by gpderetta on Sunday December 17, 2023 @04:22PM (#38676585)
Because two phase construction is a widely disliked idiom. Named constructors retuning optional objects are tolerable though.
Re:Misra C++:2023 Published by jupp0r on Sunday December 17, 2023 @05:27PM (#38677094)
Two phase construction isn't widely disliked, it's completely fine if it's used as an implementation detail of a factory.
Re:Misra C++:2023 Published by pjmlp on Monday December 18, 2023 @01:49AM (#38679892)
Only the big public C++ projects that blindly follow Google's style guide, mostly.
Re:Misra C++:2023 Published by kwant_kiddo on Monday December 18, 2023 @06:02AM (#38681120)
I don't think this is true, but I don't know this for sure. From what I have read it seems that 'only' Bloomberg, Meta and Microsoft that uses c++ with exceptions.

And since both microsoft and meta are adopting rust in their services it seems to me that they are looking for another language than C++. (why else adopt a new language?)

Following seems not to use exceptions(?)

LLVM: https://llvm.org/docs/CodingStandards.html#do-not-use-rtti-o...

AWS: (seems to be using Google's guidelines to be fair)

Webkit: https://gist.github.com/derofim/df604f2bf65a506223464e3ffd96...

Qt: https://doc.qt.io/qt-6/exceptionsafety.html

gcc: https://gcc.gnu.org/codingconventions.html#Exceptions

Unreal: (not totally sure, but I think it uses error codes internally)

Most of the embedded world. + any console game you ever played or heard of.

Re:Misra C++:2023 Published by pjmlp on Monday December 18, 2023 @08:02AM (#38681838)
LLVM has been for quite some time driven by Apple and Google.

WebKit, another Apple child.

Qt, it has to support environments where exceptions are not allowed, otherwise they would be losing customers, specially since Qt is older than C++98.

gcc, was initially written in C, and for quite long time had a mixed code base with minimal C++.

The companies adopting Rust aren't doing so because of lack of exceptions, they would still adopt Rust if the language had exceptions support (which panic and std::ops::Try kind of are), rather due to the type safety that C and C++ aren't able to provide.

You would be surprised how many games actually do support exceptions.