Home > C++ > Sandwich Dilemma

Sandwich Dilemma

In this dated, but still relevant paper, “Evolving a language in and for the real world“, Bjarne Stroustrup laments about one of the adoption problems that (still) faces C++:

Since the overarching theme of C++ is, and always has been, “efficient abstraction“, it’s not surprising that long time efficiency zealots and abstraction aficionados would be extremely skeptical of the value proposition served up by C++. I personally know this because I arrived at the C++ camp from the C world of “void *ptr” and bit twiddling. When I first started studying C++, its breadth of coverage, feature set, and sometimes funky syntax scared me into thinking that it wasn’t worth the investment of my time to “go there“.

I think it’s easier to get C programmers to make the transition to C++ than it is to get VM-based and interpreter-based programmers to make the transition. The education, more disciplined thinking style, and types of apps written (non-business, non-web) by “close to the metal” programmers maps into the C++ mindset more naturally.

What do you think? Is C++ the best of both worlds, or the worst of both worlds?

  1. hungry hungry hippo-fish
    March 3, 2012 at 8:26 am

    I have to say that I am disappointed with this post. I thought the title would outline a broad debate over the perceived differences and values between a grilled reuben and a po’boy.

    • March 3, 2012 at 9:54 am

      Sorry. Maybe I’ll address the issue in a future post. Even though Reubens rule, fried hippo fish sammiches on Friday are pretty damn good too. Speaking of fish on Fridays….. ????

  2. March 3, 2012 at 8:49 am

    Interesting question. Both C++ and Modern Java have similar processes to get from source code to machine code. And the recent (post JIT compilation) benchmarks I’ve seen suggest that they have similar efficiency.

    There are a couple of apparent differences, though:

    1. Java compiles to abstract byte code and doesn’t JIT until a library is loaded. C++ performs its optimization pass in p-code before generating machine code, trading build-time overhead for run-time

    2. Java uses pass-by-value semantics, while C++ supports both pass by pointer, reference, and value. Pass by value leads to hidden calls to copy constructors, which cause inefficiencies in code written by unsuspecting newbies.

    3. C++ makes programmers handle their own freeing of memory. Java has built-in garbage collection. The C++ approach leaves programmers vulnerable to a class of memory errors, such as leaks, double delete, freeing addresses not allocated, etc. Java GC is still vulnerable to leaks (if programmers leave dangling references), but addresses the other issues. The price is that GC can take a while (in computer execution time) and can be started at inconvenient times.

    In the end, it seems to me that Java and C++ started out being very different, and over time have evolved to be more similar.

    Charlie

    • March 3, 2012 at 9:52 am

      Thanks for your thoughtful analysis Charlie. I too think the languages have started converging to some extent. I think that Java, which was highly touted at launch as being simpler to use/learn because it was a pure OO language (which C++ was never meant to be) and had garbage collection, has moved toward C++ with JIT and, more importantly, the tainting of its pure OO roots with the introduction of generics. There’s still one major difference. Java is an entire environment whilst C++ is just a language. Bjarne Stroustrup & the ISO C++ committee have always wanted it that way (the “efficient” part of the “efficient abstraction” goal) and it looks like they want it to stay that way for the foreseeable future.

    • May 28, 2012 at 4:38 pm

      I think you stayed in the 80’s.
      Have a quick look at Modern C++. C++98/03 or C++11 (better).

      The “Newbies” argument doesn’t seem valid to me.
      You’ll find surprises in any language if you don’t know it enough.
      If I want to code ala Java in C++, I will find surprises.
      If I want to code ala C++ in Java, I will also find surprises.
      Newbies should learn and practice, then they can write production code.
      It is true that detailed knowledge of C++ takes longer than Enterprise/Web Java-like languages.

  1. No trackbacks yet.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.