149 points · by ingve · 10y ago · 154 comments
I try to avoid using C++ for any pet projects if I can help it, but I've found that being forced to use it for about 4 years really changed how I thought about relationships between objects. My Java code c. 2005 was basically like spaghetti; if I needed to operate on an object, I'd pass in a reference somewhere and call whatever I needed, and then let the garbage collector (maybe or maybe not) clean it up. In C++ this doesn't work - you will have an unmanageable mess of memory leaks and use-after-frees (or reference cycles if you resort to shared_ptr). So I had to learn how common program architectures (servers, compilers, distributed parallel programs) manage memory and assign ownership to certain overall scopes. This in turn has informed how I design my production Swift, Javascript, and Python code, or use Java dependency-injection frameworks like Dagger.
I still appreciate being able to write spaghetti while prototyping, but I've found that maintainable production programs seriously benefit from having clear notions of object ownership and lifetime.
This means, long hours of tracing, debugging, leak checks etc.
The lesson is: never ever rely on compiler, framework, language or other potentially buggy tools to think for you. Even with Rust's "hard" approach, you can get complacent and hemorrhage performance and memory until there's no turning back.
Those things were written by fallible people without hard mathematical proof everywhere, after all. Rust and especially its libraries too.
Understand libraries, their implementation and design trade-offs if you're using them.
But you don't have undefined behavior to debug, and that makes debugging easier. Trying to reverse engineer from a corrupted memory dump from a production system what the compiler did to your code and relate that back to where it went wrong is something you will have to do in C++, given enough time. You don't have to do that in Rust.
So I prefer to choose a language where I can nicely express the intent of what I code. Java is a good compromise, because it's easy to follow (even if it's quite verbose and mainly stuck in the world of nouns).
C++ is hard for that. It's an extremely complex language that got extended over the years and it takes a quite long time to master, for example compared to Java.
Yes I know, C++ is more "capable" than Java but it's not efficient in the hours-spend-per-solved-problem way of things.
Yes I know, there are you C++ whiz kids out there who are insanely fast using the language. But I experienced teams using it, and in a team C++ does not scale well. Adding more and more features to it makes it a cool language, but not one which I like to use in my day job.
And I'm not sure what I'd use it for. I use Java for the day-to-day boring "enterprise" world, twiddle around with dynamic languages for the hobby (yay to the recurrence of lisp!) and for close to the hardware stuff I'd use plain C - which is a not so bad language at all. And it's hard to beat speed-wise, writing C++ that keeps up with C is possible but not trivial.
And if I'd have to look forward I'd check out rust or go or even look at some really mindbending stuff like erlang. Parallelism can't be evaded in the modern world and using a language tailored to that is a great thing. (Yea, I'm sure there also is a C++ library implementing the actor model).
My use case for instance, is to use C++ as C plus the STL (yuck for doing everything manually in C as opposed to having neat classes that are easier to use and equivalent after compilation), useful C++11/14 features like auto type inference or lambdas, PoD structs, and little else.
It's probably not as mind-bending as you think. The actor model is fairly intuitive. The difficulty is that it's fairly easy to have unexpected behavior from simple actor rules. And the syntax is kinda funny ('.' and ',' look too similar to me), but there is elixir if you want (although I think elixir is even weirder...)
https://en.wikipedia.org/wiki/Restrict
Some compilers offer "__restrict" or "__restrict__" in C++, but it's not standard. Standard C++ lacks the feature.
Besides "restrict", there is also the matter of theory not matching practice. The norm for C++ programmers is to be at least somewhat unaware of various extra copies and allocation. You know it's true. In practice, this makes the language slower.
If the compiler cannot understand the code you write, I doubt your "coworks and gals and guys" are going to be much happy with you. Isn't code for both the compiler and programmers? >>So I prefer to choose a language where I can nicely express the intent of what I code. Java is a good compromise, because it's easy to follow (even if it's quite verbose and mainly stuck in the world of nouns).
I don't call verbose code nice, just like average people don't call verbose prose nice. It is personal whether something is easy to follow. To someone who has put in the time and effort C++ code can be easy to follow; I see no reason to denigrate one for that. >>Yes I know, C++ is more "capable" than Java but it's not efficient in the hours-spend-per-solved-problem way of things.
If I am going to spend 30 years programming, I am going to become more "capable." And I will spend LESS hours-per-problem because I have put in efforts to really understand the problems and gained insights. >>Yes I know, there are you C++ whiz kids out there who are insanely fast using the language. But I experienced teams using it, and in a team C++ does not scale well. Adding more and more features to it makes it a cool language, but not one which I like to use in my day job.
Some teams are built to use Java, others use C++. I do HPC and numeric analysis; my C++ skills is quite an asset to my team. >>And I'm not sure what I'd use it for. I use Java for the day-to-day boring "enterprise" world, twiddle around with dynamic languages for the hobby (yay to the recurrence of lisp!) and for close to the hardware stuff I'd use plain C - which is a not so bad language at all. And it's hard to beat speed-wise, writing C++ that keeps up with C is possible but not trivial.
I know what I use C++ for, as do other people. C lacks abstraction mechanism and is fragile. And it is actually not very good for today's hardware. Eigen library offers ease of use and good performance using template-meta-programming; I don't see any C library doing both. >>And if I'd have to look forward I'd check out rust or go or even look at some really mindbending stuff like erlang. Parallelism can't be evaded in the modern world and using a language tailored to that is a great thing. (Yea, I'm sure there also is a C++ library implementing the actor model).
Erlang employs one kind of parallelism, and there are others. They are suitable for different use-cases.
Look, it doesn't surprise me a Java programmer doesn't think much of C++. I get it, it is competition. I don't recommend Java to my team lightly either. But, really, you got to find better trash talk.
That said I can seriously see Rust giving C++ a run for it's money in about 2-3 years. All these things you have to learn the hard way(ownership, move semantics, dangling pointers) are dealt with up-front in Rust. They really aren't bolted on as an afterthought but a core part of the language(std::move I'm looking at you!).
I also think there's some interesting work to be done w/ language integration so you can drop Rust into Java/C#/Ruby/etc. I think this is actually the best way to drive adoption since you don't need to greenfield it. Tricky part is going to be making sure you preserve the correct lifetime guarantees but I don't think it's insurmountable.
People put too much emphasis on low level language features when promoting new languages. The pitfalls that Rust solves are known issues in C++, and there are known solutions to them and best practices for avoiding them. A lot of the problems are "solved" as new iterations of the standard come out.
To really give C++ a run for its money Rust will have to compete with the gigantic ecosystem around C++. It has libraries for literally everything. It's backed both directly and indirectly by all of the biggest software companies in the world. It's supported by debuggers, profilers, memory profilers, static analyzers, IDEs, refactoring tools, and editors. There are books, tutorials, training classes, etc.
let z = Complex<f64>::new(1.0, 2.0);
let A = Matrix<f64>::ident(10, 10);
let B : Matrix<Complex<f64>> = z*A;
let C : Matrix<Complex<f64>> = 2.0*B;
If Rust can't do scalar multiplication or real to complex conversions, it's really not usable for what Eigen or Numpy can do. Try defining the Mul trait generically for those multiplication operators, and you'll see what I mean.(yes, I know there are some syntax errors in the type declarations above - It's my opinion Rust got that wrong too...)
Supposedly this kind of thing will eventually be possible with the recent "specialization" changes, but I haven't seen anything that allows operators to work as above...
ps: Last I looked, there was fledgling support for SIMD on the horizon... LLVM supports that, so it could happen.
I mean this as a serious question, cause I see C++ being used instead of C thanks to its multi-paradigm capability and I don't know how Rust could fight that for now.
If anything I would say Rust is a tad more multi-paradigm due to its really strong functional roots(Sum types!) and nicer treatment of lambdas(no alloc like in std::function).
It's definitely worth trying and learning. I would've ditched C++98 in a heartbeat to move to Rust, but C++ is a pretty exciting and productive place to be ever since C++11.
It's also nice to just tab to your terminal and do `cargo run` and get your code compiled out of the box. Writing a decent makefile (or whatever else you plan to do) for C++ is comparatively quite a hassle.
Anyway, the lack of an IDE hasn't hampered my ability to be productive in Rust. Unlike Java, C++, et al.
I do think c++ provides an interesting area to program into but the part where you do it to avoid places with business requirements would only speak to programmers who don't yet realize that the business perspective of your code is where your career growth and potential come from (not from learning more and more advanced coding technique).
Yes and no. I think programmers get the biggest lift, career-wise, from being able to put themselves in the shoes of a stakeholder while coding and being able to talk intelligently about what they produce to non-technical people. But that's just the price of admission to work in an environment where you don't get treated like shit usually.
Beyond that, if you want to work on problems that you really enjoy then finding a technical specialization that gets you excited to go to work is a must. Then find a job where your knowledge in that area translates to business value. If that sounds hard, spend some more time socializing with other people in your industry. Consulting or working on OSS projects helped me a ton in this area.
That's the arc my career has taken at least, and I'm still early in it.
I would not agree with that. Unless you insert 'mistakenly' into that sentence or replace 'rewarding' with 'underpaid and overworked'.
I think working in games can absolutely be incredibly rewarding if you love the medium and delighting players. On huge teams individual effort can be hard to connect to player enjoyment but I've found it super fun to work on and lead smaller teams where everyone can directly impact players and get concrete immediate feedback on how your work is affecting player experience.
Unlike many other areas of software development most game features immediately impact how players perceive your game. I love the immediacy of that connection.
[0]http://www.gamasutra.com/blogs/PaulTozour/20150120/234443/Th...
I used to work on physics engines, but I was into physics engines, not games or Hollywood. Dealing with Hollywood sucks. Either they're in development and their credit cards bounce, or they're in production and they want a new feature yesterday.
Qt is another huge argument why i use C++, there is no alternative (or i don't know any other cross-platform framework as good and mature as Qt is).
I know all the copying and moving and whatnot is supposed to "just work", but as far as I can tell, there's no easy way to verify that your program is doing what you intend. There also aren't any compact guidelines -- "follow these N simple rules and the STL won't explode!". With most other languages, I can at least think through the program to figure out if it is going to do what you intend, but the way that the STL is implemented to support all the modern C++ behavior requires a brain the size of a planet to understand.
I'm sure Stroustrup and the committee can write Modern C++, but I sure can't. Not with any confidence, anyway.
Watching video from ccpcon is not the way you will learn C++. Read book from Stroustrup about C++.
I did buy Stroustrup's post-C++11 book, "A Tour of C++", which is supposed to be the compact "how to do Modern C++". It provides tips, but as I mentioned above, I did not find it comprehensive enough. Terse recommendations like "Prefer returning by value" may be great small-scale recommendations, but they don't explain how the big picture is supposed to come together.
But PyQt is not so much a binding layer as just Qt in Python, literally. It has to be some kind of semantic offense though, since I have no problem writing Qt C++ to begin with. It is just the language and style of the API itself favors C++ because thats what it is made for.
QML and Python, on the other hand? Lovely. If it didn't depend on all of PyQt to just start up a QML program and signaling into / out of it, I'd be all over that idea.
Which is probably where I am super interested on the Rust front. qmlrs showed a lot of promise last year, and while the project seems to be suffering from a lack of contributions as of late, someone will eventually pick up the reigns - I do not think any other GUI framework is even close to appropriate for Rust to begin with anyway.
There isn't. I've been looking for twenty years.
Qt is the only platform I'll use for embedded GUI development.
The fundamental problem with C++ is that it has hiding ("abstraction") without safety. No other major language does that. In C, there's no memory safety but everything is explicit in source. In memory-safe languages, there's memory safety even if there's lots of abstraction.
Rust has a brilliant idea in the borrow checker, but the type system is kind of weird, the attempt to bolt on functional programming is marginal, and error handling involves hacks such as "try!()". But with enough use, those aren't so bad. Maybe. Personally I would have preferred Python-type "with" clauses and exception handling to RAII. If anything goes wrong in a destructor, Rust cannot help you.
I assume you're not up to speed on all the constexpr work.
I mean, Objective-C tried explicit [foo release] for a long time, but I haven't heard anyone clamoring to go back to the pre-ARC world…
For Python's "with", someone figured out what to do if something goes wrong in __exit__, which is called as control leaves the with clause. An exception in an __exit__ is handled well, and nested exceptions work reasonably. Failure to get this right results in problems such as unnoticed truncated files because the status on the close after a write wasn't checked.
In the long term, there may be a reversal in the trend towards "easy" languages.
However, the performance of dynamically typed languages has also improved a ton. It's not unrealistic to think that you could write simple JS code and get about 50% of the performance of an equivalent C function, possibly more.
Things like simple loops that work on typed arrays in javascript can really give C/C++ a run for their money right now.
'C' is used in kernels, drivers and such, in tools and in embedded work.
Templates though... are nice, I guess, but the cost in compile time, ABI, code size, and ease of debugging is a lot to swallow.
Besides, with the preprocessor and M4 you can get many of the benefits of templates, albeit with most of the downsides (and a few more to boot).
But is a reason so many large native codebases go with C++. The language provides some pretty handy tools to manage code organization and complexity at that scale.
2) Templates: you don't need to resort to a horrible macro mess to implement a type-safe container.
3) Polymorphism: you don't need to manually manage vtables.
Also enum classes are pretty useful. Unlike old enums, the labels do not pollute an outer scope.
Seems like all the really high performance stuff is in C++, C, or Fortran.
Recently learning Python, and knowing some basic C for microcontrollers, I can see how OOP is really handy, thus C++.
Of course, crusty old code can be buggy yet originally working - e.g. relying on implementation defined or undefined behavior. (or even bugs in compiler or standard library implementation)
A better question would really be "What factors would lead me to believe C++ is right tool for a particular task?" Which, I think requires a bit more depth than you'll find in this article.
This is a bit ironic because using accelerated APIs means the host language could be anything and it would still be 99% as fast.
They are not without problems. Complete template definitions, default circular includes. Not supporting partial classes. Ect.