4 ms·
It's a reaction to the Boost crowd, who took C++ into template la-la land. For realistic programing: * Avoid multiple inheritance. It seldom helps and there a
by Animats 10y ago
It's a reaction to the Boost crowd, who took C++ into template la-la land. For realistic programing:
* Avoid multiple inheritance. It seldom helps and there are lots of confusing cases. Putting one object inside another is fine. If your inheritance tree is more than two or three deep, you're probably doing it wrong.
* RTTI is better than unchecked downcasting.
* STL collections are fine and should be used in preference to built-in arrays.
* Avoid Boost. If anything goes wrong, you'll spend days to weeks figuring it out.
* Exceptions are not that bad. Go and Rust now have de-facto exceptions, with all the complicated unwinding machinery, but they're called "panic" and "recover". Just make sure that anything you pass in an exception is a self-contained copyable object with no pointers to anything, because that's where ownership and allocation trouble appear.
* Don't do anything in a destructor that can fail or block. Like closing files opened for writing. Destructors are for releasing memory. Raising an exception in a destructor is not a good thing.
* Don't overload operators for anything other than math. It just confuses people.
* Streams are more cool than useful. But mostly harmless.
* unique_ptr and move semantics have promise, but it may be too soon for those features. If you have complex ownership issues, consider Rust, which has compile-time checking for that stuff.
- to3m 10y agoI concur. I don't expect that to convince anybody, but a post needs an opening, and this is as good as any. Regarding unique_ptr and move semantics, I used both quite a lot on a recent project (VC++/clang/gcc) and have been pretty pleased with the results. It can be quite hard to make things copyable sometimes, but it's usually easy to make everything at least moveable, so where in the past you might have had to use a pointer (leaving room for error) or a smart pointer (more coding, slow debug build, annoying in the debugger), you can now just always have a value. And unique_ptr is good for documenting ownership; I used it for stuff that gets passed between threads, but of course there are other options. You don't get any useful compile-time checking, but if you get things wrong then it does at least go pop at runtime in an obvious way... which is better than nothing. (As the saying goes, C programmers know the value of nothing! And that's why we appreciate it so much when we get anything better.) My two takeaways from the experience: - moving as a language feature is far more useful than copying... I suspect (though without having thought through it especially thoroughly) C++ would have been heaps better if it started out with move semantics only, with copying being only by convention - Rust has moved further up my todo list
- vvanders 10y agoSmall nit but Rust's panic is not an exception mechanism: https://doc.rust-lang.org/std/panic/fn.catch_unwind.html https://doc.rust-lang.org/std/panic/fn.catch_unwind.html Particularly you may not be able to catch all panics. Result<T,E> is the canonical way to handle errors in Rust. Also in gamedev(where this looks like this comes from) we generally avoid RTTI(for size reasons) and STL(for allocation control reasons, see EASTL, etc).
- Animats 10y agoFor both Go and Rust, it's not supposed to be exception handling. But both languages have acquired proper unwinding, so people can and do use it that way. The original idea in Go was that if you caught a panic, the program was in a bad state, and all you should do is report the problem and maybe restart the program. Like "longjmp" in C. Now, people are doing recovery without re-launching the program. That's effectively exception handling.
- joshkpeterson 10y agoComments like these are literally what keeps me coming back to Hacker News. Thank you.
- fnj 10y ago> Don't overload operators for anything other than math. It just confuses people. Do you think << and >> in iostreams confuses people? > unique_ptr and move semantics have promise, but it may be too soon for those features. What makes it "too soon" for unique_ptr (or shared_ptr)? These are the most useful and long-overdue improvements to C++ I can think of in its entire history.
- ndesaulniers 10y ago> Don't do anything in a destructor that can fail or block. Like closing files opened for writing. I'd like to know more about this. That means to RAII file types?
- Animats 10y agoRead this: [1] In C++11, exceptions in destructors have defined semantics, but those semantics are very complicated. Prior to C++11, the semantics were not well defined. [1] https://akrzemi1.wordpress.com/2011/09/21/destructors-that-throw/ https://akrzemi1.wordpress.com/2011/09/21/destructors-that-t...