Is C++ Just a Better C?

A lot of money and energy was spent in converting or writing new software in C++ during the 80s and 90s. Even nowadays, when the leading commercial language is Java, there is a lot of effort spent in learning and using C++ in several areas where performance is considered to be an important asset.

However, despite all these efforts after 20 years, C++ is not used to its full extent. Some features such as the STL have become really successful and widely accepted by the industry. On the other hand, other features such as checked exceptions and RTTI have been met with little enthusiasm.

It is necessary to understand what these features have in common and why they were not considered to be so useful as their creators supposed them to be. Especially when such features have been introduced more than a decade ago and are still nowhere near full acceptance.

Duplicate Features

One of the problems of the C++ language is that it has, more than once, introduced features that are difficult to use and/or implement. As a general rule, this is a disadvantage of languages designed by a committee. Some features end up becoming part of the standard document, but they have little support in the real world.

For example, a decade ago the designers of C++ though about the possibility of having templates defined in separate translation units from the ones where the templates are used. The intention is good, since templates take a long time to parse and compile.

However, the big problem with separate compilation of templates, provided by the external template keyword, is that it was never implemented by major compilers. The main justification is that it is a complicated feature to implement. Also, it would require modifications to the current way programmers declare and templates.

The result is that external templates are now viewed as a dead feature, something that might be useful but is not available in any of the major compilers.

Coding Standards

A related problem for C++ is that nobody uses all its features. Due to the fact that there are competing implementation for the same concept (for example, two IO libraries — the old C-base library and a stream object-based library), C++ users have to decide which option to use. This may be done in a case-by-case basis, or even by the use of a coding style.

Big companies such as Google have decide to rule out most of the advanced features found in newer versions of C++. These features include exceptions, multiple inheritance, for example. What this means in practice is that C++ is being used just as a better C, without many of the features that its creators though would be useful.

On close inspection, it seams that C++ is mostly a language designed at two levels: the first level, incorporating the ideas of the day, which change every few years with the desires of researchers. Examples include checked exceptions and run time type information. In the process, lots of incompatible ways of writing the same code are introduced. For example, are you going to use return values or exceptions? Streams or standard buffered I/O? Both have its advantages and disadvantages, so you better know what they are.

In the other hand, there is also a second level dictated by developers, as they decide which subset of the language to use. Since it is not possible to use all the provided features at the same time, one has to selectively use C++ to create new software projects. One of the ways this is done is by adopting a styling convention. In this process, developers rule out a set of features that believe are not productive.

Still, another result of this evolutionary process is that there are some features that people don’t like, but that are not easy to substitute. For example, templates are a pain to create and even to use. However most developers use them because it is the only way to do some things in C++. Somebody else could create better container classes, but that would become a huge problem for compatibility with existing libraries.

Conclusion

C++ is an evolving language. As a consequence many of its features are not well developed. Other features have been rejected by the community as too complex or unsafe to use.

A large part of being a good C++ developer is understanding what are the good and bad parts of the language, and how to take advantage of them when necessary.

Further Reading

The Design and Evolution of C++, by Bjarne Stroustrup is a great book that explain the reasoning for most of the features in C++. It is a must read if you want to understand the initial phase of evolution of the language.

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

7 Responses to “Is C++ Just a Better C?”

  1. C++ is powerful language. Yes, I too agree, its real power comes with understanding the language features and design principles. We can see few of the applications that were developed in C++ on the following link

    http://www2.research.att.com/~bs/applications.html

    There are many other books on understanding the language features (like those by Mayers, Herb Shutter, Andrei Modern C++, C++ Coding Standards, etc…)

    By Venki on May 8, 2011

  2. The problem is that C++ is _not_ a better C. It’s actually a worse C.

    C is a portable assembly language that provides minimal abstraction between you and what the machine is actually doing. Its purpose is to not get in the way between the programmer and the hardware.

    Scripting Languages like Python/Ruby/PHP abstract away everything they can: memory management, type conversions, even your entire build system (no compile stage). They build objects on top of dictionaries instead of structures, meaning you can add or remove things on the fly. It’s a completely different way of programming, where you program _to_ the abstractions, because they’re opaque and most of the implementation details become completely irrelevant. (Is the dictionary mentined above implemented as a tree or a hash table? Answer: it doesn’t matter, it just works.)

    C++ is an uncomfortable middle ground: its abstractions leak implementation details in subtle ways, ALL THE TIME. It includes the whole of C (static memory management, static typing requiring templates to resolve at compile time, pointers), but then tries to implement thick abstraction layers on top of pointers and memcpy() and stack vs heap storage distinctions and target-specific word size and alignment requirements… Its abstractions regularly fail to hide implementation details, but the _existence_ of the “abstraction” gives complexity somewhere to hide and breed… until it doesn’t work and you have to figure out why. (A lot of C++ programmers have put _decades_ into “figuring out why”, and are sometimes highly defensive of this sunk intellectual capital to a degree bordering on stockholm syndrome.)

    C is good at what it does. Scripting languages are good at what they do. C++ is big and complicated and a great way to gain employment, just like cobol was.

    By Rob Landley on May 9, 2011

  3. C++ is definitely is better C. The two good reasons not to use C++ instead of C for a new project are 1) portability 2) familiarity with C.

    C is a great language but suffers from several historic flaws: Weak type safety, reliance on macros, lack of uniform resource management.

    C++ partially fixes those defects with the following features: new style casts and const safety, templates, support for RAII.

    C++ has its share of horrible features (exception specification comes to mind) but C99 recently introduced highly questionable constructs that do not fit well in the language, for instance VLAs and complex numbers, whereas C++ is powerful enough to support them as mere libraries.

    Because C is the language of choice for someone does not mean it is the same for everyone. There are domains which require both performance and high-level constructs (e.g. video games and scientific computing) and C++ is currently the best fit for those.

    By Julien on May 17, 2011

  4. @Julien, yes, C++ has a lot of features that are helpful. It is sad, however, that it is so confusing to use in general.

    By coliveira on May 21, 2011

  5. @Rob, C++ has a lot of problems, but sometimes it might be easier to use for non-critical applications. I wouldn’t create an OS in C++, but a desktop application may benefit from it.

    By coliveira on May 21, 2011

  6. What is this about the “newer verions of c++” and multiple inheritance? C++ has always, from way back in cfront days, had multiple inheritance. If a company throws that out, especially virtual function only mixin class inheritance, then they have thrown out an important bit of what little expressive power the language has. I imagine such “safe c++” shops also toss out operator overloading as well.

    By Seren seraph on Jun 30, 2011

  7. It is not a secret that multiple inheritance is avoided by lots of users and companies. In most cases it is only allowed when necessary to implement interface-style classes that are pure virtual, almost the same way as Java does.

    By coliveira on Jun 30, 2011

Post a Comment