What’s wrong with C++

…from the point of view of someone that actually uses it? The bits from my last post haven’t even dried up yet, and another fascinating rant about C++ has popped up, this time from Zed Shaw.

What’s interesting about these rants is that the ranter isn’t really using C++. They’re C hackers, or they program mostly in Python, or they used C++ at the beginning of the 90s. So I thought it would be interesting to write about my experience with C++.

First, a little background info…

I started learning C++ about 10 years ago, first the C part and then the ++ part. Nothing serious, just toying around with small projects to learn and have fun. I didn’t use it professionally until about four and a half years ago, when I started working on a large scale C++ project. Since then I’ve used it almost daily.

Two and a half years ago I came across Qt almost by accident: the large scale project that I was working on was being ported to Linux and the port was enabled by Qt. Since then, I’ve used Qt almost daily too.

Right now I’m working on a  Qt C++ project that’s about 25KLOC in size and growing. It’s a multi-platform, threaded client-server and it’s a line of bussiness app if you’d believe it. Working on it is fun, and I’m glad that I picked Qt and C++.

Okay, enough with the history, what about C++ and its warts?

Issues that bother me in practice

  • Compile times and the include dependency specification: this is a problem that every C++ programmer will have sooner or later. It can be delayed and mitigated with a good physical design, precompiled headers and distributed compilers/linkers, but it can’t be solved without throwing away compatibility to existing code completely.
  • The small standard library: not a big issue in itself, since you can find other libraries for anything you might want to do, but it’s unpleasant to install all those libraries and make sure that they work together nicely. Using more libraries instead of a large standard library also means that you have to wait on each library vendor to port the library to different OSes/compilers, upgrade it and so on.
  • lack of reflection: I like Qt’s moc and I miss it in standard C++. I also wish it had more features.
  • lack of type inference: even with typedefs I’m typing more than I have to. C++0x will thankfully fix this.
  • localization & internationalization support in the standard library: the implementation is complicated and difficult to use. After browsing through C++ IOstreams and locales I’ve decided to just use Qt and not worry about it.
  • template error messages: they’re long and hard to understand. I usually parse a template error message in about 10-20 seconds depending on the difficulty. The worst offenders are metaprogramming-heavy libraries such as Boost Spirit. In that case, I try to analyze the code first and see what might be wrong. STLFilt can help.
  • functional programming support: this will get better in C++0x, but I can’t use C++0x now, at least not on all platforms and with the other libraries I’m using. Functors and free functions are cumbersome to use with algorithms.
  • tools and parsing: C++ has access to solid, mature tools, but those tools are much harder to develop than for other languages and generally don’t have as many features.

Issues that don’t bother me at all, but are regularly brought up in rants

  • memory management: never a problem for me. The last time I’ve had a memory leak was about two years ago and it got caught at code review. C++ has good support for memory management and good tools that can be used for runtime debugging. It might as well have GC, I wouldn’t be able to tell the  difference.
  • memory corruption: memory corruption can be both exciting and scary. Exciting because you have to go down to the memory address level and do some sleuthing, scary because it can take days or weeks to find some bugs. I usually don’t sweat these bugs, they were very rare in the code I’ve worked on, and when they occurred they were easily found. e.g: the last one I’ve solved in maybe 30 seconds, the tools have gotten very good. The worst I’ve encountered was a problem with an embedded STL in a large project that took a week to figure out and only because of my lack of experience at that time. Some people love debugging.
  • multi-threading: it is hard in most languages. The trick is using higher-level libraries such as QtConcurrent, Intel’s TBB or OpenMP. Sure, if you’re doing PThreads it’s going to be painful.
  • string formatting and string support: I generally don’t use iostreams or std::string, but when I did I found them acceptable. Unicode support was the biggest issue, but “there’s a applib for that”. Now I use Qt’s strings and streams; they’re as easy to use as Python as far as I’m concerned. Don’t want Qt? Use boost::format or any other of the nice formatting libraries.
  • templates in general: I use them when needed to break dependencies, write generic code, etc. I try to keep template code manageable, without going into the metaprogramming stuff; they are one of my favourite C++ features.
  • exception rules: I don’t see any problem with exceptions in C++ as long as you’re using RAII. Other languages (e.g.: Python) use them extensively for error handling.
  • STL & Boost: I’ve used both for a lot of time and I find them very useful. Contrary to popular belief, most templates don’t use metaprogramming techniques. You don’t have to install Boost, it’s mostly headers. A large part of Boost became TR1 so it should even ship with your compiler now.
  • const: This one came from Zed, I don’t think I’ve seen that many people complain about const. Personally, I constify everything that I can, even const type* const.
  • everybody learns a different 10% of C++: strange, because I use all of it and always have. It’s not that hard either, I’m not a genius… Sane projects have coding guidelines and architectural documents. If your teammate can waltz right in with a template metaprogram or is generally macro-happy and it doesn’t raise eyebrows at code review, you have bigger problems than using C++. I’ve had to fix two C++ projects so far; my only job was to do what it takes to make the programs work and ship them to the customer. Most problems were memory leaks, superfluous use of casts, functions that didn’t compile on all platforms and so on. Fixable. What I couldn’t fix – and had to patch up as best as possible – were issues with the program logic and architecture. Those were not caused by C++.

Ok, but what about…

You probably have your favourite C++ misfeatures.  Odds are that they don’t matter than much in practice, or workarounds are available. Maybe I just got used to them and consider them a C++ tax.

66 thoughts on “What’s wrong with C++

  1. I like compiled languages because of the power they provide, to create code that runs fast, something interpreted languages can’t beat.
    To get the benefits of both compiled and interpreted languages I start creating a new programming language, which I called Mewa ( http://mewaide.blogspot.com/ )

  2. Great post! I love to read about everyones opinions on C++ because the project that I’m working on right now is in C++ (using Qt also). Pretty much all of your points ring true to my experience as well.

    I have, however, been having some problems with memory leaks. This is probably because I’m a relatively inexperienced C++ programmer. You mentioned there are a lot of good tools for diagnosing these problems. Would you mind listing a couple of the better ones?

    Thanks for the post!

  3. I vouch for this. If you can’t get your C++ to the point described above you haven’t absorbed it enough to have earned the right to complain. What gets me is /how often it comes up/, everyone complains it seems.

    (But let’s not tell the complainers about rvalue references shall we? I’m still struggling with those (but then I haven’t used them yet).)

  4. Very well said. Speaking as someone who was a C++ language lawyer of sorts at one time, and has since moved on to primarily Ruby development (but without the giant chip on my shoulder most ex-C++ users seem to carry), I think this is all bang-on. One of the most cogent practical critiques of C++ I’ve ever read.

  5. C++ has really improved over the past 10 years. I started learning it about 15 years ago, when there were very loose standards and incompatibilities galore. I’ve seen steady improvement, and with a standard, STL, Boost, and the other libraries that have come into existence recently, C++ is actually kind of fun. Memory management and corrupted memory can be tricky, but again, library support has fixed much of that, and running a debugger or valgrind truly helps weed those issues out.

    As to program logic, absolutely, no language will fix poor logic and design.

    I really don’t get all the hostility – maybe I’m dinosaur. :/

  6. As someone who uses Qt pretty much every time I work on some C++ project I have to say at this stage I almost consider it its own language in some ways, and a massively extended standard library in others. It defines it’s own types and collections (QString, QList, QHashMap), provides it’s own networking API, OpenGL API, Regex, XML parsing, Multimedia.

    Not that any of this is bad, I love it. But at the same time I find that when I discuss C++ and how I don’t have problems with it I often wonder if I’d feel the same way without Qt :P .

  7. I don’t understand why most C++ rants rarely touch upon the OO factor. The most important aspect of C++ to me, is OO stuff such as polymorphism, design patterns etc, which can be quite hard compensating for in C if you have been indoctrinated in the OO way and is the MOST important reason i would/do use C++ for a project instead of C. Memory management etc. is equally hard/easy in both languages.
    Another point I have to say becomes evident when looking at source code (good source code, from big projects with good developers). C code almost always looks very elegant and well designed and easier to follow than C++ source code. C++ source code often becomes more clumsy and ugly a lot faster.

  8. “I constify everything that I can, even const type* const.”

    Yep, that’s not a problem at all. :-)

  9. Thank you. Yours is the first critique of C++ I’ve read in a long time that parallels my own experience of the language (it’s been my primary language since 1997).

  10. Thank you for finally providing some sane reflection on C++. I suspect most of the haters haven’t even given it a fair shake, they just jump on the band wagon. Granted, C++ doesn’t make sense for everything, but with Qt it becomes an awesome language for writing large GUI applications.

  11. One more for the list:

    I wish in overriding methods you could call the base method with base.foo(), or something else that automatically finds the next base method, instead of needing to specify the base class explicitly.

    Thanks for the post!

  12. ” Odds are that they don’t matter than much in practice, or workarounds are available. ” — Or they exist in the 90% of C++ you don’t use and don’t know that you don’t use it.

  13. My biggest beef with C++ is that everything it adds to C is designed to make code easier to write, and none of the things it adds are designed to make code easier to read.

    I find C++ much harder to read than C. You can usually take a C program, and examine some function in it and right away more or less tell what it’s doing. It’s not typically couched in a mountain of invisible context. With C++, you’ve generally got to read a *lot* more code to be certain you’re really understanding what a given method is really doing.

    And, I find that C++ programs tend to have about 50% of the complexity created by the problem domain, and about 50% added by the programmer to create some elaborate class hierarchy or network, whereas the comparable C program will be more like a 70/30 or 80/20 split, with the larger fraction of complexity due to the problem domain, and the lesser fraction introduced by the programmer. In other words, it seems to me that a typical C++ program will be carrying an extra load, and not a small load, of complexity which has nothing to do with the problem being solved.

  14. I’ve use boost’s threads and mutexes a lot for multi-threaded programs. Like memory management, if you use them right, there’s really no problem.

  15. “…everybody learns a different 10% of C++: strange, because I use all of it and always have…”

    Really?

  16. Thanks for the comments guys. :)

    Aaron: What platform are you developing for? Both on Linux and Windows the runtimes have some debugging options that can be turned on to detect memory corruption and leaks (e.g: the Visual C++ debug heap). Then there are the free tools like custom mallocs (e.g: dmalloc), mpatrol, valgrind, LeakDiag, UMDH, DebugDiag, and the more expensive tools such as BoundsChecker for Visual C++, Insure++, HeapAgent, etc.
    But the most important “tool” is the way you design your code. Think carefully about object relationships and use smart pointers.

    If you don’t mind waiting, I plan on writing an entire blog about memory management and maybe you’ll find that more helpful.

  17. Just saying thanks. I’m glad there are some programmers on reddit (assuming you posted it there) that aren’t stubborn C++ haters.

    C has its uses, as does everything else from Java to LISP. I’d even argue VB has its use. But anyone who says C++ is a “bad language” is absolutely wrong.

    So yeah, just saying thanks. I keep finding these anti-C++ articles that are just depressingly stupid (Linus usually makes pretty good points, but even he puts his foot in his mouth pretty often), whereas this one I absolutely agreed with. Thank you.

  18. So I’d like to respond to memory management. You are correct: memory management in C++ isn’t hard. Memory leaks and memory corruption are rare enough that it’s not a big deal. Memory leaks can also happen in garbage collected languages (and, indeed, are probably more common, since people don’t think about memory).

    The point of memory management and garbage collection is that it allows you to structure code in ways that would otherwise be impossible. Functional programming is a big example — most functional code is effectively impossible in an unmanaged language, since there isn’t a good way to clean up the memory used in closures.

    You wouldn’t notice if GC was added, but you don’t program in those ways. People who use GC languages do notice when it is removed, because suddenly a big part of their tool set disappears.

    It also allows for much more concise and readable syntax. Compare:

    ((derivative sin) pi)

    to:

    f=derivative(sin);
    f(pi);
    deallocate(f);

    Aside from that, properly implemented GC (rare as it is) is generally faster than manual memory management. There are two reasons for this. First, objects can be freed in bulk, at the same time. This allows for optimizations that are impossible with conventional memory management. Second, the runtime can move memory around, and restructure it for cache coherency. This can give a substantial performance boost. Sadly, most GC systems don’t bother.

  19. I’ve been using C++ for fifteen years. It has paid for my house and my Volvo.

    C++ is a problem in a large environment with mediocre programmers. Memory leaks and rogue pointers, in the wrong hands, can devastate project schedules. Java and C# were invented to keep the inept from shooting themselves in the foot.

    As for “managed C++”, look up the metaphor of the 28-cylinder Pratt & Whitney R-4360, impossible to tune, ripping itself from the wings:
    http://en.wikipedia.org/wiki/Pratt_%26_Whitney_R-4360

  20. Hugo: There are already languages that can be either interpreted and compiled, while easily interfacing with C.

    Most of them, however, behave nothing like C.

  21. What motivated you to use Qt/C++ over a web-based solution?

  22. I love C++ but I don’t love running teams who are writing in C++. People who are really good at C++ (I don’t count myself – I just like it) say how if you do everything correct – it is brilliant. They are right. Getting a team of people – some who are great, some not so great, to do everything correct is near on impossible. IMHO C++ is a high end language for high end projects.

  23. SteveC/theZagnut:

    Strange, I thinks the exact opposite is true. I can think of very few examples where pure C code looks more elegant than the same code with C++.
    Given there is always good and bad code, there are so many things you can not express in a sane/elegant way using C.
    That starts using string operations, matrix or other math operations (complex numbers for example), graph based operations, anything that does implement a class like system in C, ….
    Only with pure low-level stuff pure C code can have it’s elegance (e.g. memory tricks).
    Other than that I am pretty sure I can give you a more “elegant” approach for a pure C code snippet you give me (besides that the C code usually works in C++ too…).

  24. I agree with Alex Turner. Out of the people doing C++ out there, 90% of them should not be doing it at all.

  25. I found alot of people dont use boost, or other similar monsters. The boost libraries and headers are godlike for anything c++ , and i found it makes my language experience far more flexible and powerful than other languages i need to use frequently (the .NET, and other argued platforms).

    It has quirks, of course, as does any tool but i agree here, C++ is awesome.

  26. Yes, people who don’t know how to use C++ generally make awful messes with it. That’s not the language’s fault, though.

    As for flaws in the language, I have had a bit of trouble recently with the inability to assign a stream to a variable at run time. This can be programmed around by using pointers, but it is still annoying.

    On the other hand, C is a truly horrible language. I’d probably have to find another profession if I was forced to use C to write large programs. And you can make terrible messes with C as well. Take the Windows kernel … please.

  27. I’ve been coding in C++ for six or seven years now and I regularly make use of the OO-features of the language such as polymorphism, and I often use other high-level features such as template programming. However, I’d like to think that my “style” of C++ coding is fairly close to what I’d write if I had to use straight C. I also const-ify everything I can get my hands on. When I want to make a pointer type, I always think about whether I can add const twice to the type name as well. One of the features that I wish the compiler would warn users about is use of const_cast (or C-style casting away of the const modifier) because in my mind if you’re doing something to eliminate a const modifier on a variable or object, then that implies that you’re doing something which is not designed for.

    I enjoy the language because it gives me so much control over the machine, and because it’s so (fast) and close to the metal. I personally prefer to avoid large template libraries such as Boost because I frankly never use many of the features that it supports and I’ve already learned how to do the same things but through my own code rather than someone else’s. To me, that ability to understand what the code is doing because I wrote it is more important than the ease of which I can simply include a pre-written library.

  28. Alex, good post. I have been using C++ since last 10 years and using Qt since last 6 years. Many times I found Qt very helpful. I found Qt collection classes better then STL classes.

    I am surprised that you have not encounter any memory leak in last 2 years. Can you please give some tips on how to avoid memory leak.

    One more problem that I found is that even though you free the memory, it is not released to the system immediately. There are some cases when I need huge memory block to do some temporary processing and I release it immediately, but the application does not free up the memory at the same time. Any idea about this.

  29. I’d say ‘const’ really doesn’t belong in the list of weaknesses, it is one of the strong points of C++ other languages don’t have. Proper use of consts avoids many many problems with unforeseen side effects, faulty assumptions by the one who’ll modify the code after you moved to the next employer.

    Memory leaks are not a problem of programming languages, but of programmers. If a natural language makes it possible to say nasty things in that language (and they all do), it doesn’t make it a bad language. Likewise, you can code badly in any language and personally I don’t think languages should protect against things that can easily be avoided using good coding practices.

  30. If I have a beef with C++, it would be templates, which I regard as an unacceptable detriment to legibility. (Pace a commenter above, templates also increase the amount of code one must read to understand someone else’s program.)

    The team I manage has developed a large library of useful classes that my people are expected to use when appropriate. It’s made the use of third-party libraries largely unnecessary. Yes, we’re a narrowly focused team (real-time simulation)…but then, aren’t most teams?

  31. I’ve been ussing C++ for little more than 6 years. I’m currently a Smallworld Magik GIS Developer but that doesn’t mean that I’ve given up on C++ at all I’m still writing Applications in C++ regulary.

    I’ve worked on: C++, C#, Java, Magik, pascal and Delphi.
    and never have I came across a language that could match up to the power off C++ some has come really close though.

    To me its not about how quickly I can write a program but more obout how much control I have over what my Application is doing and how its doing it.

    my latest project was Digital Signage solution in C# using CsGL(OpenGL wrapper for C#) and DShow.net I was ammased by the speed but missed the control I had with C++.

    call me a control freak but C++ is the language for me…

  32. I have been using C++ for over 20 years (and C for nearly 10 years before that), and I am a co-moderator of Usenet group comp.lang.c++.moderated (I was one of the founding moderators, and I continue to actively moderate that group along with 5 or 6 co-moderators, who come and go). I have indeed run into pretty much all of the problems you mentioned in the rants. One of the things that got me out of the “10% of the language” was that I spent about 7 years teaching it, which forced me to get into all of the “corners” of the language. Unfortunately, the training business has all but disappeared. Too bad, because training was the one thing I enjoyed more than programming (at least indoors with my clothes on).

    Nice to see C++ bouncing back. In 2002, I actually had a recruiter hang up on me when I told him I was a C++ programmer with more than 15 years experience. I remember that there was an ad that year for 8 C++ programmers in the DFW area, which caused a traffic jam that made the evening news here.

    I have the great fortune to be working full-time on a graphics toolkit written in C++, but the main problem I’m running into in the legacy code base (and it is HUGE) is memory leaks. The main problem is the lack of a consistent ownership policy for pointers. I hope to retire in about 5 years, and I hope that C++ continues to flourish at least that long. I hope that I will be able to use C++0x, but revamping such a huge code base may not be practical.

    I’ll add a rant. Going from VC8 to VC9 was a giant pain for this project because there was a change in the “decorated name length” for a set of layered typedefs used in some templates. It took me over 2 months to re-write the code to eliminate one layer of typedef so that it would actually compile.

    Oh, and another rant: I have to support Linux, also, and I often find that a change that works just fine in Windows breaks the Linux build, and it is a little more effort that I think it ought to be to get to a solution that works on both platforms.

    I truly love C++. It’s made me a good living for over 20 years (over 30 if you count C). I don’t think that any other language would be suitable for a high-performance graphics tools product that needs to be multi-platform (and not just in the Microsoft sense).

  33. The one of few things I dislike in C++ is the lack of explicit keywords for implementing or overriding a virtual function.

    You never know, if a virtual function in class is to be overwritten, or if it overwrites/implements a function from base-class itself.
    The problem arises, when base-class’ virtual (not abstract) function is renamed, the compiler can’t support you in finding of overwritten function of all child-classes.

  34. I have used C++ for several years now, and C for many years before that. C++ is, in many ways, the ultimate heavy-duty compiled language, IMHO. It is certainly not for beginners or irresponsible developers, and it requires coding standards and a passion for learning the underlying details and mechanisms (e.g., the side-effects of template use, polymorphism, etc.). However, it is extremely powerful and flexible, and the debuggers and other tools have truly matured.

    All that said, if I were starting a new project from scratch for the desktop, I’d consider C#, since it brings together most of the good features of C++ and Java while dropping some of the messiness. In summary, when I hear people complain in general about C++, I have to wonder what they’d use instead. I know of a codebase of some 2MLOC of Perl, which I cannot imagine trying to understand or maintain, whereas 2MLOC of C++ is, if reasonably well-organized, is actually manageable. (Of course, that 2MLOC of Perl does more and is probably equivalent in functionality to 6MLOC of C++, but I’d rather be thrown into a project maintaining the C++.) Sure, Ada is good for large projects, but C++ has far more compilers, libraries, tool support, etc. FORTRAN? C++ is far more modern, safer, and more manageable. Are there any other compiled languages to seriously consider for large projects?

    Oh, and Hugo, please spare us! I know you mean well, but if you can’t find what you’re trying to develop, ya ain’t lookin’ hard enough :-) If I were you, I’d think about other avenues for your efforts. Maybe contribute library functionality or tool support to Google Go or Haskell, or port the .NET framework to another platform. Or, jump on the “D” bandwagon, write a compiler for PHP or Javascript, etc. Programming languages are like microprocessor architectures; the world really doesn’t need another one, short of a complete paradigm shift.

  35. “everybody learns a different 10% of C++: strange, because I use all of it and always have. It’s not that hard either”

    I find this very hard to believe. C++ is quite a large language, with lots of odd syntax and features. Just look at some of the more hairy template programming possibilities. Yes, it _is_ that hard, at least, hard to get right and harder to understand it when someone else has written it.

    Although I’ve certainly not written probably 1% of the C++ you have, I started learning it 12 years ago (with the unrealistically-titled “Teach Yourself Borland C++ Builder in 14 Days”) after a little exposure to C on the Atari ST. For a while, most of my programming was in C/C++, then Java in 2003, back to C++ and (of all things) Tcl in 2007 for a quick job, and lately Python and Ruby because they’re fun.

    You’re correct to point out the importance of tools for debugging and especially tracking down memory leaks/stack smashing, so perhaps having poor exposure to those has unfairly coloured my judgment of C++, but the fact is that I never enjoyed programming in C++ quite as much as I have enjoyed Python and Ruby in particular. Mainly because they offered transparent garbage collection (yes, I _did_ notice it and it was really nice not to have to worry about “who deletes this?”) and they did not suffer from the hassles of the C/C++ include system, having to split class definitions into header and code files and order your include statements carefully, and perhaps most importantly, the had a syntax was much less unwieldy and verbose than that of C++.

    For me, the question was not so much “What’s wrong with C++?” but rather “What’s better in some other languages?”

  36. C++ is a powerful tool, altough it can be abused at impressive limits, such as unreadable template metaprogramming magic. But there are some neat tricks for getting maximum performance such as compile-time polymorphism. Yes, templates are GOOD, used properly.

    And of course, I use Qt extensively. Now that is LGPLed there is no reason to go with another framework (wxWidgets is MFC-like, too much C-type macros). It’s excellent, and high-performant. I prefer Qt containers over STL, altough they can mix nicely (mostly).

    The worst thing of the lenguage is compiler speed — however, there are some tools to decrease build times (precompiled headers, parallel make (e.g. JOM), distributed builds, etc).

    I always try to develop using a good header dependency design, but sooner or later, a project build will crawl.

    And finally, I don’t find any advantages using C over C++. Code generation for C++ is top notch in the best compilers (MSC/GCC) and there is no performance gain using C.

  37. Me likes to think of c++ as a system programming language. Hence the stuff like reflection, localization & internationalization, functional programming, etc, belong to other development platforms. So anyone might ask me where would “I” use c++? Well, I’d want to use it where my program would want to talk with a device, or it has some memory intensive tasks to do. Because I believe c++ is optimized for these. When I say ‘optimized’ it doesn’t mean that things like memory management, or file handling, etc, are easy to code from a programmers perspective. So you have to keep vigil in that area.

  38. I don’t usually comment.. But… You should try C#.. It has all the advantages of C++, without all the headaches.. Plus, it adds the functionality that C++ will always be missing…

  39. I have written more C++ than any other language, but many times I will develop in other languages because the speed of development improves because the language lends itself to a the objective.

    However I think that C++ is still the best all purpose language available.

  40. SteveC… while C++ can add code bloat… there is a big reason for it. C++ is setup for large projects with big teams and for enabling highly maintainable code. The structure is primarily focused on providing an expendable, re usable infrastructure. If its done right it can be very readable and easier to follow.

    The trick (which sadly many C++ programmers still don’t understand) is that you need to make your classes invariant. That is small classes that do one job well… classes which make sure the objects they create are always in a correct state.

    For example… take stl. If you tried to implement vectors in C… it would be hard to read and the code would not be able to check many things at compile time to make sure things are correct. A vector is an invariant.

    Most classes you write in C++ should be as simple as vectors. If you do that your code will be easier to follow then C because it provides standardized rules that turn semantic understanding into syntactical understanding.

  41. Seriously, why are you even using C++? If you want C++ to be Java or Python or Blub so badly, why not just use Java or Python or Blub in the first place?

    C++ is designed the way it is for a purpose, and throwing in all sorts of other features will just turn it into a different language. “Oh, I have to type a few more letters to declare something than I do in Python…” That’s kind of the point in a statically bound strongly typed language. If typing speed is your programming bottleneck, your problems are trivial and you have no understanding of the purpose and power of a langauge like C++.

    If you dislike C++ so much, stop whining and just use a different langauge.

  42. I have several decades with C and C++. C++ is fine, for most jobs,
    just remember that C++ is just C with permanent training wheels
    attached. If you just going to ride to the local store and back it’s
    O.K. Same basic approach as Pascal and Java, protect the programmer from
    his or her self. But good C++ is never better than good C. I have been
    programming since 1968, what ever that means in today’s market
    , which I admit is not very much.
    …Dave

  43. Peter:

    Thats not a realistic example, think stack allocations or RAII in C++:

    derivative(sin)(pi);

    … if you deallocate manually today, you’ve missed the last 5 years.

  44. This is the best list/rant so far.

    I agree with every single word in it.

    I use C++ some times, but mostly code in web languages (js/css/php/html) and delphi/lazarus and c (more c++ then c).

    I’ve always complained against the issue with strings, but the rest of the list simply continues where my rant ends.

    Cheers,
    Christian Sciberras.

  45. I learned C about 1990, C++ about 1992. I stopped programming in both about 2001.

    Having worked in 20+ computer languages over my career, my major objection to C++ was that it was too complicated, and didn’t work for decent businesses. The only people who still should be using it a people who need to build next to the hardware, like device driver makers.

    Let me go into some detail here. If you look at a language “grammer” which are basically the parser rules of the language, the larger the grammer, the harder the language is to learn.

    This is also true in real languages, as well, but let’s get back to computers.

    So, when I started programming in C, the K&R book put the language grammer at about 6 pages. A 6 page grammer is simple enough that your average software developer can master the language structures and all the combinations in about 3 months. They can be productive in the language relatively quickly.

    Java was about 7 pages when I started learning it, is probably closer to 8 pages now. Again, more difficult than C, but definitely “learnable”.

    C# appears to be about 8 pages as well.

    C++ grammer from the Stroustroup book was 13 pages. It took most programmers about 2 YEARS to master the language. Most, like me, got into it enough to build objects, copy objects, reference objects, but never really mastered all the templates, multiple inheritance and more obscure parts of the language. Many of us former C people didn’t break from our old “C” habits with pointers instead of references, and really struggle with it. Many architects in companies restricted the use of the more obscure features (like multiple inheritence and templates) in order to keep the code “standard” and not become a complete mess.

    About the same time as C++, two other components came out, one being a “standardized” “machine language” layer that exists both in Java and in Microsoft languages. This separated the language from the hardware architecture, which then effectively made “compiled” languages interpreted through this middle layer. If you need raw computation speed for some activity, use C++. Most people don’t need it. They need good algorithms and work through an interpreted layer. The other component that came out was the “standardized” library. All Java effectively uses the same interfaces for GUI, for file read/writes, and most importantly, for common data structures, lists, trees, maps, dictionaries, etc.

    C++ stayed with the compile to the machine hardware, and not all compiler vendors kept to the standards. Microsoft Visual C++, until the last versions, did not support exceptions correctly, and some template features. If you “stayed with the safe stuff”, it was OK. If you tried to port over Standard Template Library code, and needed reverse iterators, you were pretty much hosed. C++ never standardized on libraries, so some people used STL, some used RogueWave, some used the Microsoft API libraries.

    Now, the complexity in software development has gone from the language itself, to the software library support. The interview questions are not “have you used templates”, but more like “how would you implrement this data into a map or dictionary library object”. The libraries take years to master, but most anyone can learn the language itself quickly.

    That being said, I’ve moved on to integrating software server tools together, using as little code as possible, and mostly reusing built components.

    Software is now like construction. You get paid to the the job done, not to argue over what the best language is. Any tools that help you get the job done more quickly, get you more money more quickly and software is now a mature industry, a BUSINESS rather than a research project, so be productive, so you can get paid. Get ‘ER Done!”.

    Glen Austin
    Solution Architect

  46. I’ve been doing C++ in some form or other since 1985. My current platform of choice is now C++ and QT. Together they are an elegant solution to solving programming problems without having to solve coding problems.

    To the one user who asked about memory management, the trick I use is the concept of ownership. Whoever allocates something, de allocates it. I use this even in threaded producer consumer patterns. The producer allocates the object, the consumer merely marks it as deletable so that the producer can delete it in a sweep. Many people will get tripped up in a multi-threaded app where one thread allocates and another thread deallocates. This is obviously in Win32.

    Anyway, nice article.

  47. I think people do not understand object oriented programing nature of C++. If they think that C++ == C with classes, then result is complete mess. Object is a king in C++. Forget about arrays – vectors and lists are your friends, forget about dynamic object initialization – use static objects or smart pointers etc… When you embrace true object oriented programing there is no big difference betwen C++ and C# or JAVA.

  48. Good article – interesting to see how Qt has smoothed the platform and localization issues.

    My only issue has been getting the right people – design and coding practices being the biggest problem.

    I can see why other languages (namely Java / C# ) are very popular for new projects especially when coupled with accessible design patterns implementations.

  49. Yea every language has its problems and the technologies are evolved over time. People are preferring .NET technologies in these days. Even it’s productive, we’re paying for performance. Also .NET versions are not really compatible each other. The CTS is too huge and there’s steap learning curve for the proper usage of the classes and libraries available. Also people are not really bothered about the basics of computer science. Everything built-in place. A good designed C++ program will not face much issues with memory management and corruption. If too many problems are happening, it’s not the right technology to use within the context. Choose something else!

  50. Well,

    I’ve experienced none of those problems as quoted.CPP lets you go close to the hardware. This makes it both Powerfull and Dangerous. I always try to limit the number of Base Classes, so that indeed, a new developer has only to trace back a few steps to find underlying functionality. There was an idea in the 1980′s of derriving a new class for every trivial extension. This makes for Tons of Documentation to read.
    Alternatively, We add most extentions to the Base Class, Derive a Class which initialises Some Properties, and then use Asserts to ensure that the correct derived class is invoked.

    Derivation of classes is usefull for Framework Developers.The MFC Framework wizards stand out as examples of this!
    It is on rare occasions indeed that we ourselves derrive classes from others.

    Microsoft does much the same in MFC! Most of the CDialog facility is for instance implemented in the Base Class of CWnd.

    No, I Like CPP, and have nothing against it.

    Regards,

    Bram van Kampen.

  51. Some people says that C++ is old and complicated language, but can’t be more wrong because it’s a really fast language, not like graph on java or C#, nothing about run-time machines, it´s direct to processor comunication, fast and easy.

    As last comment, I Agree with the guy that says that C++ is not for lassy people, I invite them to study and test a really fast language.

    PD. I have tested C, C++, java JDK 1,3->1.6, C#, phyton, VBasic, VBasic.net, I keept with C++ when I can.

  52. I also agree with Alex Turner, but I think it is not the language that makes it hard to work in teams with people with largely different knowledge.
    Of course some languages are easier to grasp than others, especially if they contain some less common programming paradigms. I can not avoid thinking that not C++ is the problem but rather object orientation, which is at best confusing, if improper applied. Declaring a multitude of classes, deriving from each other, forces you to grasp the behaviour and variance of all those classes, if you want to understand how some application works. And you seldom find any rational documented in code or elsewhere describing the why something was designed the way it is. You are lucky if the api of the classes is completely and correct describes. You are near heaven, if the same api is fully tested. And get it right, every method and member variable you can access, either directly or in an derived class, is some sort of api.
    One thing, that may make C++ harder than other languages like Java, might be the fact that it provides powerful and complex features like templates whose intention must be fully understood to apply them properly. I learned: If in doubt, don’t use the feature. If you must use it anyway, understand as much as possible about it. But this is not only true for language features but also for algorithms, design patters, libraries, tools and everything else involved in software development.

  53. PUSH ALL

    For those that note that C++ can be hard to read. I’ve found plenty of C code since the 1980s, that required a caffiene binge and a steady stream of cussing to work through.

    It’s my experience that you can write crap code in any language.

    There is no reason why C++ cannot be elegant. I think that as you move to languages that have a larger poputlaion of programs, you’re going to see a wider variety of coding styles and levels. C has become a niche market and the coders left in it, have likely all honed their craft well.

    I will say though, that assembler beats C for elegance, and machine code is the lagnuage of the silicon gods. :P

    POP ALL

  54. There are good reasons not using mm/gc based languages:

    How long takes one gc run with allocated 200GB RAM?

  55. For me C/C++ means speed which i believe no other languages can ever provide. If you don’t like C/C++ you don’t know to use it.

  56. Hi
    I’m not agree with your arguments as drawbacks of C++ (or further C++0X) language.
    Plus some of those arguments sounds partially or totally wrong to me.
    Finally please consider the most arguments you seems to advance are absolutely not related to the language fundamentals but to third party libraries, operating system, compilers, etc …

    New programmer people are now very greedy with “easy to use” languages but – in my opinion – those languages are really not good at all for shaping their programmer skills in terms of understanding what happens at hardware level behind your thousand lines of “easy code”.

    Regards

  57. Admission: I don’t write C/C++ anymore, though I still port code for projects when something open source can assist a project. I started around 1988 and mostly stopped in 2004.

    I don’t miss subtle stack overwrites, you know, just a few params get trashed and when you add debug, it doesn’t happen, because it’s packed differently compared to the stripped production version!

    I don’t miss large product porting, as some coders over the years were still novice and didn’t know to use endian handling libraries!

    I don’t miss having to mix / fix BSD versus SYSV versus POSIX libraries (I make no mention of all the #ifdef win stuff). Nor learning new teams’ great library X. Old teams’ great library Y was just so very different. Seems every successful product has a complex mix of old code, and C/C++ often has some serious baggage.

    I still like C. I prefer to pass pointers to functions and chains of objects than do OOP. Both ways seem to have serious amounts of debug time, with bigger code bases, in C/C++!

    I still don’t like that C++ OOP gets big, and complicated, fast. By 2 million LOC contributed to by a team, C++ gets darned incomprehensible (for my small brain). Biggest I’ve worked on was 5 million LOC (weather forecasting product).

    I was around for the introduction of Structured programming (anyone do Jackson at college?) That was to defeat the spaghetti code that was around from before its general acceptance

    before I go, just a thought. Not my own, so I’ll say that I quote Rich Hickey – “mutable objects are the new spaghetti code”

    Hmmm, is it only me here thinking that higher level languages (something say without the likes of C inside) are the way to go!?

    regards
    Alex
    code monkey and wordsmith at Lexecorp.com

  58. Finally managed to approve all comments. Yes, even the cowbell one.

    I must say that even though I try to read most news and discussions about C++ – from lambda the ultimate to blog posts – I’m impressed with these comments. Industry veterans, newbies, lovers, haters have all posted…

    Some quick thoughts:
    * I did program in C# and Python. Generally I use Python in interactive mode to explore/do prototypes. PyQt is great.
    C# is a language that I could use instead of C++ for most things, but the funny thing is that I don’t need to. C++ works for me, so I keep C# as a backup.
    * At this point I’m more interested in architecture/domain knowledge/customer interaction than languages. I am familiar with D, haskell, Ruby and the ideas behind them, but I just don’t think that using any of those languages will give a productivity boost over C++ and Python.
    * For my current project we picked native over web due to skillset and requirements. Part of the app will
    probably be served on the net when needed. Personally, I’m a fan of native, but I will use webapps when it makes sense.

  59. OK, to tell you how much i loved C++, i wouldn’t want to program on anything that isn’t Borland. Since so many languages came out after C++, I went back to assembly for microcontrollers and linux for console C++ programs. I think it is the best language I could think of even now. I hate MFC. Long live to the Main() function.

  60. NOTHING is wrong with C++

    to me, reflection is another antithesis of object oriented programming … similar to “properties” (C# exposing your private parts to the public)…

    asking the question “what type are you” IMHO, violates the notion of polymorphism and the ability to dynamically alter the code behavior by NOT having to ask “what type am I/are you?”

    do these features cause the very same spaghetti code in C cause by bad software engineers (not the language’s fault).

  61. Really interesting thread.

    I have the deepest respect for people that have the experience and knowledge needed to handle a complex subject like C++. After 10 or so game productions in C++, I still regard the language way to cumbersome to produce stable and maintainable code in. These days I simply try to avoid C++ projects, since I feel more productive in other languages (here are some reasons that I mostly agree on: http://www.fairyengine.com/articles/cppvscsharp.htm). Another important point is that C++ seems to fall behind on the tools side, that is, when compared with the amount of tools available to backup a JAVA or C# production.

    When you really have to go the C++ route, then a problem I have experienced is when a complex subject – like C++ or multi-threading – is not regarded complex by the team members. The “I am such a brilliant coder” attitude doesn’t really cut it, when the team needs an “I really need to focus on learning the C++ language” attitude. Synchronize the team and learn from the best e.g. read Effective C++ and discuss what other readers think (e.g. http://www.vterrain.org/Implementation/effective.html).

  62. I programmed professionally in C++ for over ten years. At the time I loved the language and was seriously proficient in it. When I finally switched to C#, I initially missed a lot of what C++ offered, but I got over it and now use C# for many things.

    In the work I did, I needed reflection so much that I actually wrote a library and system that implemented reflection on top of C++ in 1992. I think that people don’t appreciate enough what reflection (lightly used) can accomplish for you in certain circumstances. In particular, reflection allows for a much abbreviated approach to unit testing with tools like NUnit that rely on reflection heavily.

    -Kelly