From C to C++

As a long time C programmer and compiler writer (I even wrote a book on C), when I pivoted away from running ImageCraft (see below if you missed the announcement on our website), I knew I would need to beef up on my C++ skills.

Like most C programmers who learned about C++ in the earlier days, I know about classes, inheritances, virtual methods and all the Object Oriented concepts. As a compiler writer, I even know how I would go about implementing them. In the last decade, even though I was not using C++ much, I kept up to date with the development news, and read about templates, template metaprogramming, and other new C++ features. Without actual usage experience, some of them just sound very odd, for example, template metaprogramming sounds like one of those “if they are so weird, they better be very useful” items.

Spoiler alert, you can write a Fibonacci function or something similarly, and have the compiler evaluate it at compile time using template metaprogramming. Indeed, C++ template metaprogramming is Turing Complete, a fact that was surprising even to the designers.

No matter, I dived in and did a small project writing a Lisp interpreter in C++. I even used lambda functions! Then at my new $dayjob, I pushed to implement the project in C++, utilizing the power of the Standard Template Library (STL) and more.

After six months of development, the upshot is that an equivalent C implementation would probably be at least three times as large in source code size, and would have taken at least two or three times as long to write. Using STL and utilizing the “algorithm” helper functions, the code is readable, succinct, and without baggage.

While I have not done outrageously weird stuff with templates, judicious use of template does exactly what it promises: compile time type checking polymorphism. Unlike Object Oriented Programming using virtual functions et al, there is no runtime penalty.

On top of that, utilizing the implicit memory allocation and deallocation in the class’ constructors and destructors and lifetime scoping rules, there is no explicit calls to “new” (or “malloc”, in C parlance) or “delete” even though dynamic data structures are being used. It’s almost as good as runtime garbage collection, without the overhead.

STL’s ease of use and the runtime performance of the class design allow us to come up with innovative data architecture that is superior to traditional designs. While I cannot discuss the product in any details, further posts will explore some of these features of C++.

Scroll to Top