5 ms·
IIRC C++ is dangerous because of how it does exceptions, and you might have exception-inducing code creep in unnoticed, esp. given that the standard library use
by dnautics 5y ago
IIRC C++ is dangerous because of how it does exceptions, and you might have exception-inducing code creep in unnoticed, esp. given that the standard library uses exceptions. I could be totally wrong about this.
- loeg 5y agoYou could (and would) disallow exceptions in a Linux kernel use of C++. This could be enforced with something almost as dumb as grep. Exception-less C++ is still perceived as having more cons than pros. You likely would not use the userspace stdlib — much like how Rust’s core/alloc crates are significantly limited here.
- vlovich123 5y ago-fno-exceptions is a thing. No grep required.
- deleted 5y ago[deleted]
- mgaunard 5y agoDisallowing exceptions pretty much removes a large part of what makes C++ good. Exceptions are necessary to enforce class invariants.
- jeffbee 5y agoNo? You could just enforce invariants by panicking, which is what the C linux kernel already does.
- bregma 5y agoHow would a "panic" differ from an "exception"? Keep in mind what happens in C++ if you do not catch an exception.
- jeffbee 5y agoExactly my point.
- loeg 5y agoIf you allow exceptions, people try to catch them.
- mgaunard 5y agoyou'd catch and handle the exception at the adequate point inside your subsystem. The same argument could be made with return values: what happens if you don't handle them and just panic on them.
- woodruffw 5y agoLinux tries pretty hard to use panics as an absolute last resort: there are a lot of invariant failures that are better handled by disabling drivers or subsystems, marking an impossible response as illegal and just trying to muster on, etc. Having exception-worthy invariants become panics would probably be a nightmare, at least for idiomatic C++: every nontrivial ctor (including copies and moves) might be a panic site.
- jandrewrogers 5y agoEh? A lot of modern C++ development explicitly disables exceptions, they are unnecessary whether or not they should be used in practice. For more low-level systems-y code, there are good reasons to avoid them. While there are arguments over when and how to use exceptions, I think this is the first time I've seen them positioned as a killer feature of C++.
- mgaunard 5y agoModern C++ is very much about exceptions. Disabling exceptions gives you C with classes, which is the old style. I write ultra low-latency system-level code for HFT, and I certainly use exceptions.
- jandrewrogers 5y agoI write ultra high-throughput database kernels. We use just about every feature of C++17 and beyond, particularly since so much of the implementation is generated via metaprogramming scaffolds. I don’t use exceptions at all, mostly because it is a poor abstraction for this type of code. Exceptions aren’t all upside. Clearly, modern C++ has very little to do with exceptions, or I would have required them or found them useful at some point. I’m not saying exceptions are not useful, but their centrality to the language is empirically false given the vast number of modern C++ code bases that don’t use them and lose nothing by it.
- pjmlp 5y agoWhile I disagree with disabling exceptions, modern C++ has all the goodies as Rust (minus pattern matching) for using return errors instead of exceptions.
- _flux 5y agoHowever returning errors from failing constructors would not be very clean. C++ also does not have decent macro system, which is leveraged in Rust error handling (the derive system).
- pjmlp 5y agoTrue, but for that you will need to be using classes to start with. Yeah macro like tooling in C++ is clunky, but on the other hand I think Rust goes overboard with macros.