David Brown’s Blog

David Brown’s Blog

David Brown  //  Software engineer/Jazz musician.

Jul 28 / 11:42am

Re-learning C++

I first learned C++ somewhere around 1990, as a student, in a programming course. The main thing I remember from this time was that CFront at the time used a non-C++-knowing preprocessor, which occasionally caused weird problems with syntax errors in comments.

About 6 years ago, I tried using C++ again to build a unit test framework for a filesystem I was writing. It was probably a bad idea to use an important project as a place to learn C++, and I underestimated how much the language had changed. I ended up writing the test in OCaml, which helped with the type safety. I did spend a lot of time writing bindings to the C code I was testing, and it turned out to be a disadvantage to other members of the team, who hadn’t ever worked with ML languages, let alone functional languages.

About a month ago, I decided to actually learn a modern version of C++.

Changes

The changes I mainly had to learn where:

  • Namespaces
  • Multiple inheritance
  • Exceptions
  • Templates
  • The STL

Of these, the STL probably took the most time to learn. The other mechanisms are largely used in similar ways in other languages, although the specifics of multiple inheritance in C++ are different than other languages I’ve used, they seem to be coherent.

Misconceptions

I had some fairly serious misconceptions of C++ that I am glad to have been able to get past.

Strong typing

C++ is a fairly strongly-typed language, much more so than C. Although it allows C-style casting, it offers plenty of mechanisms to not require it, and the compiler asserts fairly strict type coherency.

What had confused me is that the template mechanism does not enforce type constraints. Languages such as Ada, or Scala have strongly typed generics (Java is less so), which requires a fairly rich type system, and also tends to force type relationships when they aren’t completely necessary (if a generic wants to use a feature of the type parameter, that parameter must be restricted to a type that supports that feature). Templates, on the other hand, are resolved at each instantiation. This is more flexible, still just as strongly typed, but tends to produce amazingly poor error messages.

Garbage collection

C++ does pretty much shun garbage collection. Although Boehm can be bolted on to a C++ app, it isn’t a style of programming that is commonly used in C++.

However, C++ makes up for this by providing full control over construction and destruction of objects. This allows for full memory management. The disadvantage is that it is harder to manage sharing of objects, which that usually requires some type of smart pointer that does reference counting. There does tend to be a bit more copying of objects with the C++ style, and I’m not completely sure of the tradeoffs between the extra copying, and the extra work of a garbage collector. It likely depends on the particular application.

Binding to C

Interfacing to C code and libraries is clearly where C++ wins over pretty much any other higher-level language. I suspect this is the major reason for the success of C++. The kind of programming I usually end up doing (systems type, such as backups) requires me to bind to system-level calls for I/O and such. In C++, I am able to call these functions directly, without having to worry about structure formats and calling conventions.

Conclusion

I’m not certain how much I’ll be using C++ for programming projects. I’ve started rewriting some small parts of JPool in C++. It is a good exercise in learning the language, but I still have quite a bit of learning to do before I can determine if it would be efficient language for writing code. I still like Scala quite a bit, and will definitely keep my Scala version of the backup software alive. Perhaps I will create a restore utility written in C++ to make it easier to restore from a rescue disk.

There still seems to be a lot of crappy C++ code out there. This is probably mostly because of the popularity of the language, not any inherit feature of it. I shouldn’t let the poor code I have seen deter me away from what benefits the language might offer.

0 comments

Leave a comment...