23 ms·
Why would you learn C++ in 2016?
- RivieraKid 11y agoI don't know why anyone would think C++ is dead. If you need top-notch performance, there's often no reasonable alternative.
- ljk 11y agostupid question: why do people prefer C++ over C? is C considered dead?
- santaclaus 11y agoI miss generics and RAII when I have to use C.
- marshray 11y agoC gives you very very little in the way of tools to create and manage abstraction. Some programmers love this and for some projects it may be appropriate. 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.
- ljk 11y agofrom a java background is it better to jump into C++ instead of C?
- PeCaN 11y agoI'm going to say no, because C++ seems closer to Java at first but it's really not. You'll probably spend too much time learning about templates and its object system instead of learning to think in the things that make C and C++ unique—pointers, pointer arithmetic, explicit handling of error conditions, low-level control of memory, preprocessor, etc.
- marshray 11y agoYes. Be sure to jump straight into the most modern C++ practices and avoid thinking of it like C (with all its focus on non-type-safe pointer manipulations). Other people have other opinions, of course. Care most about the opinion of your boss and team mates. If you show up with a C++ style far beyond what they are used to they will think you are a nut.
- PeCaN 11y agoRAII is nice—I hope GCC's __attribute__((cleanup)) gets included in C2x. 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).
- ant6n 11y agowell, santaclaus wrote 'generics', not 'templates'. So presumably that means containers like std::vector, or std::unordered_map. Those are pretty easy to deal with.
- ArkyBeagle 11y agoThe furniture in C++ seems better, or at least more numerous - boost, std::, Qt.. . This being said, if you make your own furniture, I still rather like 'C' better for that. 'C' is used in kernels, drivers and such, in tools and in embedded work.
- overgard 11y agoI don't think anyone agrees one which C++ features are worthwhile, but I think most people end up missing some feature of C++ when they fall back on C.
- Ace17 11y ago1) Resource management: you never have again to risk forgetting a call to "delete/free". 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.
- wmu 11y agoI would add namespaces to your list. They're extremely useful to organize entities of a program. No more prefixes to functions and types names. Also enum classes are pretty useful. Unlike old enums, the labels do not pollute an outer scope.
- deleted 11y ago[deleted]
- fideloper 11y agoAlmost everything is a crud app. Game developers are notoriously abused in compensation and work hours. 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).
- Aaronontheweb 11y ago> 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.
- brutopia 11y agoCan't read on ios due to social media buttons overlapping the text.
- vvanders 11y agoMostly spot on. 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!).
- 0xCMP 11y agoI agree, but again that's for new code. Many times you'd be going in to an existing codebase like he mentioned. Especially in the areas he mentioned like Financials, HPC, or Compilers. You might get away with Games because even though the engines are reused the code is mostly written to be thrown away to make it faster.
- vvanders 11y agoYup, that's why I say 2-3 years. It's going to take some time. 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.
- jlarocco 11y agoThat sounds incredibly naive. Three years isn't a long time, especially when C++ has a 30 year head start. 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.
- lossolo 11y agoI agree 100% with second point. I enjoy writing c++11/14 and it's not my primary language. 10 years ago i would say no to c++ but today it's like writing in different language. 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).
- truncate 11y agoHow about Qt bindings to other languages? Last I checked for Python (like 4 years) ago, the biggest problem I had was with licence (GPL). But yeah, compared to Python, using C++ also gives me system threads which might come handy every now and then for CPU intensive stuff.
- lossolo 11y agoI don't know about python bindings, but i was looking at Go bindings. Anyway for me it feels natural to just use modern c++ with Qt, it works for me, i do not feel like i am loosing productivity.
- zanny 11y agoThe problem with the Python Qt bindings, in my experience, is that PyQt is not... pythonic. I like Pythonic things, like Flask, over less-Pythonic alternatives, like Django. At least in principal. And yes, I know Django has gotten better about it over the years. I need to try a new project in it at some point, especially considering Flask's atrophy. 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.
- 11y ago
- nostrademons 11y agoA big advantage of learning C++ is so that you understand the concepts of ownership and lifetime, along with type abstractions and using types to enforce invariants in how your API is used. 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.
- lossolo 11y agoI think Rust will teach you out of the box a lot more about concepts of ownership and lifetime, i know i learned a lot from Rust.
- nostrademons 11y agoI found learning Rust to be a lot easier having learned C++11 first. Move semantics, RAII, borrowing (const references), mutable borrowing (references), option-types (pointers), and iterators are all C++ concepts. The big advantage Rust gives you is that the compiler handles a number of things that required manual bookkeeping or coding conventions in C++, and doesn't let you screw them up.
- pcwalton 11y agoBut you could equally say that all those C++ concepts are really Rust concepts. So I don't see how that's an argument that C++ is easier to learn than Rust. The strict checking seems to me to be a win, because the compiler tells you what you did wrong instead of having to debug undefined behavior (e.g. dereference after move of unique ptr).
- Paul_S 11y ago"Video game developer is considered to be one of the most rewarding jobs in a software industry." I would not agree with that. Unless you insert 'mistakenly' into that sentence or replace 'rewarding' with 'underpaid and overworked'.
- shinymark 11y agoI've been lucky in my career in games and have worked for some great people. Now I'm my own boss and try to treat my employees fairly. It makes me sad to hear about mistreatment in games. 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.
- deleted 11y ago[deleted]
- Paul_S 11y agoThat's good to hear you're baulking the trend. Do you develop your mobile games in C++?
- Joof 11y agoI would love to do game development / programming. The horror stories about long hours, low pay, lots of crunch all have driven me away from it so far. Gamasutra[0] did a nice article about how crunch makes games less likely to succeed (among other things); I hope this kind of stuff changes the mindset of the industry. [0]http://www.gamasutra.com/blogs/PaulTozour/20150120/234443/The_Game_Outcomes_Project_Part_4_Crunch_Makes_Games_Worse.php http://www.gamasutra.com/blogs/PaulTozour/20150120/234443/Th...
- Animats 11y agoOnly if you're really into games. 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.
- bradford 11y agoI haven't used C++ in about... 12 years. I'll admit ignorance about the updates that have occurred in recently, so I've got a general question: How is the compatibility between different versions? Specifically, would a C++ 11 compiler be able to compile legacy code written for C++ 03?
- AstralStorm 11y agoYes in 99.9% of cases. The main trouble could come from the angle of accidental move (rvalue cast) somewhere invoking a broken/wrong move constructor or assignment. 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)
- smitherfield 11y agoShort answer is yes (long answer is what the other guy said). The standards committee never makes breaking changes (a big reason why the language is so bloated).
- akuma73 11y agoWhen Moore's Law ends (imminently), performance and performance/watt will be harder and harder to come by. In the long term, there may be a reversal in the trend towards "easy" languages.
- tachyonbeam 11y agoThere already is a "reversal", to some extent, in that statically typed languages are making a comeback. 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.
- Klathmon 11y agoyou are already there for some admittedly contrived situations. Things like simple loops that work on typed arrays in javascript can really give C/C++ a run for their money right now.
- sbov 11y agoFor the average programmer it ended a decade ago - most of us suck at concurrent programming.
- wcrichton 11y agoI doubt that easy languages will disappear. Instead, compilers will get smarter at compiling/synthesizing low-level/parallel/distributed code from high-level languages/specifications. See a lot of the recent work out of Stanford on domain specific languages with Terra [1] and its derivatives (e.g. Ebb [2]) as well as Delite [3]. [1] http://terralang.org/ http://terralang.org/ [2] http://ebblang.org/ http://ebblang.org/ [3] http://stanford-ppl.github.io/Delite/ http://stanford-ppl.github.io/Delite/
- wmccullough 11y agoThis is a logical fallacy. More than likely, easy languages will become more performant.
- madengr 11y agoI'm in the middle of C++ For Engineers and Scientists, learning about STL and vectors. Though I'm an EE and learning it for software defined radio, specifically GNU Radio. 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++.
- wcrichton 11y agoIf you're going to write a compiler, please, for the love of Bjarne Stroustrup, don't write it in C++. There are so many better modern languages for writing a compiler: OCaml, Haskell, Rust, and Terra to name a few. You really want features like algebraic data types and a sane package manager in your language of choice. In my mind, the only use case for C++ is interfacing with existing C++ libraries/codebases. All other tasks can be better relegated to superior languages.
- copperx 11y agoGolang's compiler was originally written in C++, so people are actively avoiding your advice.
- f2f 11y agono, it wasn't. the go compilers initially came from Plan 9, which hasn't seen any C++ code ever :)
- copperx 11y ago"Came from Plan 9". What does that mean?
- holmak 11y agoPlan 9 was a research OS that was sort of intended to be a "next generation" Unix. The Golang project is led by at least one person who worked on the Plan 9 OS, and overall it seems to be heavily inspired by Plan 9's design. https://en.wikipedia.org/wiki/Plan_9_from_Bell_Labs https://en.wikipedia.org/wiki/Plan_9_from_Bell_Labs
- copperx 11y agoYes, I know that. But I don't understand the implication that Go came magically from Plan 9. I recall reading Kernighan stating that the first Go compiler was written in C++ before bootstrapping.
- p0nce 11y ago> The appearance of GPGPUs along with parallel computation frameworks such as CUDA and OpenCL created demand for C++ programmers with this kind of expertise. This is a bit ironic because using accelerated APIs means the host language could be anything and it would still be 99% as fast.
- Joof 11y agoMaybe many of the problems they work with are also CPU bound? C++ programmers also tend to be somewhat more familiar with how memory is transferred to graphics cards, but anyone could potentially learn in any language. AoS or SoA?
- ant6n 11y agoI think C++ was dead from like 2005 to 2011, then it started respawning (to deliberately borrow a term from fps-shooters ;-).
- deleted 11y ago[deleted]
- gozur88 11y agoYou learn pretty much any skill because it's in demand and people will pay you to do it. Of course there are some things you'd rather do or not do independent of money, but it's a good place to start. 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.
- kszatan 11y agoAs the author of the article, I can only say that I agree with you but I just wanted to write a little bit longer version of "Hey, look, C++ is alive and there are still interesting jobs involving using it!". Your question is something absolutely different, and yes, it should be given much more in-depth thinking.
- niccaluim 11y agoI'm surprised to find no mention of embedded development. C++ seems to have a foothold there.
- kszatan 11y agoYou're right, but I just couldn't realistically enumerate all the places where C++ is used. There will always be something missing.
- maxxxxx 11y agoI think with C/C++ you learn how the machine really works (you could argue that Assembler would be better but C/C++ hits a sweet spot for me) . A lot of of people who have grown up with GC languages don't seem to be aware that under the hood there is still some C code that allocates and releases memory. There is no magic.
- deleted 11y ago[deleted]
- partycoder 11y agoinsert citizen kane slow clap gif here
- Animats 11y agoI tend to think that the C++ standards committee went off into template la-la land a few years back. Everything has to involve templates, "metaprogramming", and excessive gyrations within the type system. If you want to compute at compile time, C++ templates are a terrible way to do it. 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.
- pcwalton 11y agoYou'd prefer explicit "with" clauses even for, say, reference counted types? 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…
- Animats 11y agoI mean "with" for opens and the like, not structures. Python "with", not Pascal "with". 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.
- pcwalton 11y agoRight, I'm referring to Python's "with". How do you write reference counted smart pointers without RAII?
- GFK_of_xmaspast 11y ago
- froh42 11y agoThe target audience for the code I write is not the compiler but my coworkers and the gals and guys who will maintain the stuff after I leave a project. 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).
- deleted 11y ago[deleted]
- userbinator 11y agoMy preference is to use C++ like a C with some extra useful features, and not "go full C++" by trying to "do everything the C++ way". C++ has a lot of features and is growing but, contrary to what a lot of evangelism seems to say, you are not obliged to use all of them. Use a feature when it makes the code simpler and the effort reduced, don't try to use one and complicate things just because it's a new feature that seems interesting.
- rjeli 11y agoThis is how I feel. Even as a Unix/"Pure C" fanboy, just having the STL is a huge time saver.
- joshsyn 11y agoD is a better language than C++.
- capote 11y agoPff, E is better than D any day of the week. Trust me; I'm a programmer.
- nickpsecurity 11y agoI'd consider learning it to work on a critical FOSS projects already done in C++, make money with it, or write language analysis/transformation tools to make C++ apps safer automatically. Little reason to use it in a new project without legacy C++ code.
- hga 11y agoAny insight into why Genode went with C++? That, plus swearing to never program in it again after doing so for a decade, rather curb my enthusiasm towards it.
- nickpsecurity 11y agoJesus... No I didn't know. So, add that to the list of reasons I might grudgingly learn C++.
- joshsyn 11y agoI will take a look at C++ when they get rid of header files.
- rileymat2 11y agoI actually kind of like header files. I find that they can nicely separate external interfaces from internal interfaces. They also can summarize an interface nicely. They are not without problems. Complete template definitions, default circular includes. Not supporting partial classes. Ect.
- ftclausen 11y agoWhat are some good resources for learning C++ with an eye to these modern practices?
- MattHarrington 11y agoTry Kate Gregory's Pluralsight courses.
- overgard 11y agoI entirely agree with this -- in a lot of ways C++ is truly awful (though it's getting much better); but! It's the only practical solution for a lot of interesting work. I think when people are doing something hard they always reach for the pragmatic solution rather than the principled one, and C++ is nothing if not pragmatic.
- wmu 11y agoThe worst thing in modern C++ is metaprogramming. It's powerful technique, but the syntax is awful, programs are unreadable & unmaintainable. Moreover, compilers don't help with debugging such code, diagnostics messages rarely tell anything useful. Not to mention frequent ICEs, at least in MSVC. :) As a programmer who already knew functional programming and have some experience in SML and Ocaml, I found C++-functional-programming really annoying.
- sprite 11y agoAs someone who hasn't programmed much C++ in the last 10 years, what's a good book to get caught up with all the new stuff?
- zig 11y agoI like both "A Tour of C++" by Stroustrup and "Effective Modern C++" by Scott Meyers. Though, the latter is a bit dense if you are unfamiliar with c++.