The misunderstood one

I’m a usual follower of C++ related blogs and newsgroups and still I’m impressed how much this language is misunderstood after 10 years of a precise standard. I’m actually starting to ponder if we are ready for C++0x while a lot of people yet misunderstand C++98 and C++03.

I often end up discussing with my colleagues about the fact that they prefer a less powerful, but more reliable and less obscure language like C. I know that C++ isn’t one of the easiest languages to master correctly and I also know that it has some problems about compilers often changing their behaviour and with slow templates compilation time. All things that C++0x is trying to make easier or faster with extern templates, move constructors, delegate constructors and type determination.

C++0x will be a quite complex and advanced language, which will give the programmer a lot of control over the compiler, but still after 10 years of existence of the standard C++ and formal STL libraries you can find posts like this which confuse good design practices and theories with runtime optimizations.

Having the stack container with the pop method returning void is exception neutrality not a runtime optimization. Strong exceptions safety means that the operation has either completed successfully or thrown an exception, leaving the program state exactly as it was before the operation started.

What would happen if we have a T pop() method returning the head after removing it? We might have the copy-constructor of T fail throwing an exception and find that after removing our head we also lost it forever. Having it just remove the item means that our code might only have success removing our item or fail to remove our item leaving the state exactly as it was before.

This is one of the first issues raised years ago, an issue discussed in depth since the article of Tom Cargil in 1994. An issues explained by Meyers and Sutter in their books and available in a lot of community blogs like the boost one. And still this isn’t clear even to long time C++ developers.

C++ is a multipurpose language, having all that freedom of movement requires more deep knowledge of the language itself. Shouldn’t we change how C++ is taught and more strictly enforce some practices before moving to a new standard? As a sporadic C++ teacher I often end up with this question…

13 Replies to “The misunderstood one”

  1. “C++0x will be a quite complex and advanced language”.
    It’s probably true that the more advanced the language, the more complex it has to be. But I think the problem people have with C++ is that it has layers of complexity that are not related to how advanced it is, but to its history. Compatibility with C and previous versions of C++ has always been a major source of complexity.

  2. Good point. I’ve believed C++ is well-designed but not-well-taught. But we do need an evolving and standardized C++.
    I think improving the language and teaching it are two almost distinct problems which both should be done well.
    One of the characteristics of C++ language, which is one cause of the problem of teaching and learning it, is being so huge! After years of coding in C++ I still see new features, and even new syntaxes(!) of the language time to time.

  3. Indeed, there seems to be a huge issue with C++ courses. An insane lot of C++ teachers in fact know, and teach, C.

    I’ve heard of “Accelerated C++”, by Koenig and Moo (but I haven’t read it). It seems that’s it’s the only book designed to teach C++ to beginners. OTOH, an insane lot of books try to teach a mix of C and C++. So indeed, a few good books for beginners would be a good start. Heck, even a comprehensive list of DOs and DON’Ts would be quite nice: DO use std::string, DON’T use strcpy.
    (Yes, there are lots of good C++ books out there. But most of them are only useful when you’re not a beginner any more.)

    C++ has lots of features, and some of them are useless most of the time. I don’t think that should change: those features are sometimes useful (though only to be used by experts), and that’s a huge part of C++’s power. If you want a language with less features, there are lots out there, starting with C# and Java.

    Of course C++0x will bring new features, so the list of “features you musn’t use unless you know exactly what you’re doing” will be a bit longer. But it won’t change the problem much.
    OTOH, new features can actually help: as soon as boost::shared_ptr becomes part of the standard, we can officially say “‘delete’ is evil; don’t ever use it, period”.

  4. >> What would happen if we have a T pop() method returning the head after removing it?
    > Or you could clone the head, then remove it if everything went smoothly, and
    > afterwards return the clone, all in one operation…

    As C++ currently doesn’t allow to return something allocated inside a method by reference you would still create a copy of it when returning. You would be able to check if the first copy fails, but again the temporary returned object might fail to construct as before. Only by returning by reference or with move constructors this problem can be “removed”.

  5. There’s an long history of large, complex languages. And each of them have ended up a footnote in the world of software engineering. Witness Ada and PL/I. As another commenter stated, if after 10+ years developers are still not able to be effective and productive in a language, then the language is flawed.

  6. “If even experienced developers have trouble understanding a language that’s been stable and standardized for more than decade, at some point I think you have to start to consider the possibility that there’s simply something wrong with the language.”

    That’s because the average developer gets more stupid by the year.

  7. Answer to KM:

    Note that there is a language based on C++, where you can assign the garbage collector to the classes you wish, and that has the same framework as C#. It’s called C++/CLI. I’m not sure what it’s worth, but it’s basically C++ with some extensions, that address most of your criticisms.

  8. I’m getting a bit bored of stuff like this. It’s a language for the real world. There is nothing close to C++. I see two reasons for that.

    First it is backward compatible with itself right back to C. You know if you write something you will always be able to reuse it in later version of C++ with little or no work. This is very powerful, you must be a noob or an idiot if you don’t see the power of this. Big code can take decades to die, if it does at all. If something has been running on many different architectures for over a decade it’s good, and chances are it’s thin and fast compared with anything you are about to write. (Though of course sometimes new code can be fast, thin and cleaner because all the evolving was done by its predecessor (read second point))

    Second, you can build ivory towers in it, but at any point you can take the gloves off and punch down to the metal. If I need to get a fix out by yesterday I don’t want to be jumping through hoops due to some tower designer missing something. There maybe no nice way without redesigning the tower, I don’t have time for that! A quick bit of smashing and duck tape and it’s working. C++ gives you that.

    In summary people don’t like C++ for its strengths! It keeps legacy (you’ll feel differently when your stuff is now old and using legacy that is doing the job perfectly well). It doesn’t force you to work in a certain way (you’ll feel differently when in language which does and what you have to do doesn’t fit).

  9. Yes. I know about C++/CLI; I’ve even used it, but mostly for glueing unmanaged C++ (Coin3D) into a C# application.

    It works quite nicely; too bad its about 5 years too late. Although… it will be interesting to see what Stan Lippmann and Herb Sutter can achieve with it.

    C++/CLI was a latecomer for proper WinForms support; I have no idea about WPF in C++/CLI; lets wait and see how quickly proper LINQ is integrated into the language. Notice that despite Microsoft’s statements to the contrary, the newest .NET extensions always arrive in C++/CLI significantly after C# and VB.NET.

    You know, if you look over the comments here, and at other C++ comparison posts, you see an interesting breakdown:

    * about 10% are new to C++ and need guidance;
    * about 60% have left C++ behind and moved on
    * about 30% are happy with C++; half of these people are ardent supporters who can’t understand why anybody would want to move on.

    Its these last 15% who irritate me. There has never been a one-size-fits-all language, although C++ came close before obsessing on templates and generic programming.

    Basically, a “popular” language goes through three or four stages:

    1. “The voice in the wilderness”, where one or two ardent supporters really hype it

    2. The early adopter phase, where the alpha programmer types jump on board and start the language on its upward rise.

    3. The mainstream phase, where “almost everybody” uses it; its at this point where reality overtakes hype.

    4. The popularity decline phase, where disenchantment sets in, and everybody realizes that this isn’t “The One”

    If the language is fortunate, there may be a fifth phase, wherein it finds and settles into a niche.

    C++ will never go away, just as FORTRAN and COBOL (and PASCAL). It just will never be as popular as it once was. Same thing will happen to Java and C#.

  10. Almost forgot!

    For those who can stomach the comparisons, don’t forget to take a look at the C++ Frequently Questioned Answers:

    A website constructed by a C++ enthusiast who became disillusioned and decided to write about it.

  11. Good to see the FUD machines are at it again. Lets face it, good languages will always be complex. C++0x will definitely have changes for the better.

Leave a Reply

Your email address will not be published. Required fields are marked *