4 ms·
Nice to know but arguably real-life C++ code using error codes written today won't (shouln't) look anything like their C code. Ok it might not change the outcom
by stinos 10y ago
Nice to know but arguably real-life C++ code using error codes written today won't (shouln't) look anything like their C code. Ok it might not change the outcome of the performance tests but still it would imo be more interesting to see the results of something like
llvm::ErrorOr< int > func0() {
const int num = random() % 5;
if(num == 0) {
return func1();
}
//etc
}
Also I'm not too sure the 'because call stacks are usually not this deep' (for depth of about 66) claim is justified, depending on the type of application.
- htns 10y agoThat jumped at me too. A pointer to a pointer to a struct is not a very common error code strategy.
- deleted 10y ago[deleted]
- baq 10y agointerestingly this is similar to how Rust does it: it has a Result<Type, Error> trait. https://doc.rust-lang.org/std/result/ https://doc.rust-lang.org/std/result/
- self_awareness 10y agoI think Rust got it from OCaml: https://realworldocaml.org/v1/en/html/error-handling.html https://realworldocaml.org/v1/en/html/error-handling.html
- barrkel 10y agoWith the try! macro, it's got the test and return built in. Further, if the return value is large, the caller will allocate space for it and pass the address of the space as a hidden parameter. If you've got rich return values, the final code will be pretty much isomorphic with the example C code (it wouldn't be a pointer to a pointer, though; it would be a pointer to a struct).
- danieldk 10y agoNote that Rust now also has the ? operator, which can be a bit easier on the eyes than try!: https://github.com/rust-lang/rfcs/blob/master/text/0243-trait-based-exception-handling.md https://github.com/rust-lang/rfcs/blob/master/text/0243-trai... https://blog.rust-lang.org/2016/11/10/Rust-1.13.html https://blog.rust-lang.org/2016/11/10/Rust-1.13.html
- saurik 10y agoFWIW, C++ has this really cool feature they call "exceptions", which is a language-level implementation of Result, try!, and ? that is automatically included as part of any function that would otherwise have to manually unwrap and pass along an error in languages like Rust; a really great bonus is that because the compiler has implemented this feature and abstracted it from the developer, in addition to it removing a lot of boilerplate from your code, it allows for an extremely fast table-based implementation they call "zero-cost exceptions" that can almost always completely eliminate the CPU cost of having to constantly check and forward error information.
- kaoD 10y ago> really cool feature Arguable.
- izym 10y agoYes, people are quite aware of that, and if you check the comments you'll see points made against it. Also, ? is very much language level.
- kibwen 10y agoCondescending sarcasm is unwarranted. People are well aware of what exceptions are, and the arguments against them amount to more than just performance.
- sidlls 10y agoI've implemented a Result-like error code construct in plenty of C++ projects and disabled exceptions precisely because I don't think they're a good way of handling error states in code.
- barrkel 10y agoI'd expect the compiler to allocate space for ErrorOr on the stack of the caller and pass an address to this space in a hidden parameter. In effect, the implementation func0 would probably be typed a bit like this: void func0(llvm::ErrorOr<int> *__result) An advantage of passing a pointer to the error that you may be missing is that if you can pass this location all the way down, you can eliminate redundant copies / moves / assignments. This is harder when the return value and error value are combined in a single tagged union, so there will be a lot more data shuffling code all the way back up the stack.