Why Learn C++

Written by on November 7, 2022 in C++, Programming with 0 Comments

This is the third and the last article in the series “Why Learn Language-XYZ?”. I talked about Prolog and Lisp earlier. The present article is on C++.

Once you have gained reasonable proficiency with Prolog and Lisp, you are ready to learn C++! In my view, C++ is a complex language and requires sufficient maturity (and patience) to master. But the effort is well worth it.

So what is special about C++?

Strongly Typed

Unlike Prolog and Lisp, C++ is a statically-typed language requiring the programmer to specify the type of variables and function signatures (the “auto” keyword permits automatic type inferencing by the compiler in some cases) when they are defined. It is also strongly-typed, with clear rules to check compatibility of types when types are mixed. This allows the compiler to generate efficient code, and also detects potential bugs before the code is executed.

Continuously Evolving

C++ has a rich heritage of over 40 years! Its creator Bjarne Stroustrup implemented “C with Classes” in 1979 and the language has since then been growing steadily. It was first standardized in 1998. Several updates and enhancements have been made after that at fairly regular intervals. The last official release was “C++ 20” and the next one, “C++ 23” is expected next year. Every release adds interesting features to the language without sacrificing what C++ is known for: efficiency!

Multiparadigm

C++ is a multiparadigm language. It supports Procedural and Object-oriented paradigms (both single and multiple inheritance). Although it is not a Functional programming language in the sense of Lisp, it is possible to write elegant functional-style programs in it [1]. A knowledgeable C++ programmer can use the right combination of these features to craft a program that is reusable and maintainable, without sacrificing performance.

Generic Programming

“Templates” in C++ are quite versatile. They allow writing generic code that is type-safe at the same time. And because they are Turing Complete, it is possible to do exciting “metaprogramming” [2]. In practice, combining Templates and Inheritance allows one to model abstractions elegantly.

Support for Concurrency

The Standard Library has excellent support for asynchronous, concurrent and parallel programming. There are both sequential and parallel versions of many of the standard operations such as “search”, “sort”, “transform”, etc. When used with care, concurrency can yield significant performance improvement.

Obsession with Efficiency

One of the main reasons to choose C++ is the control one has over writing efficient code (efficient algorithm comes before code efficiency). The built-in primitive data types allow the developer to define and use variables that are just right for the context, and thereby controlling the amount of memory used by the program at runtime. Function calls can be “inlined” as appropriate, to eliminate call overheads. Automatic garbage collection is not part of C++ and hence the developer controls the lifetime of every object. It is even possible to take over memory management by defining custom allocators as needed. Although it is generally considered unsafe, “pointer” gives direct handle to raw memory. Additionally, the “alignas” keyword and the recently introduced “[[no_unique_address]]” attribute help the compiler optimize memory usage of composite structures. Finally, many compilers allow embedding of assembly language code within a C++ program, giving greater control over the code generated, in addition to supporting a wide variety of optimization switches.

For the record, C++ follows the “Zero-overhead” principle:

“The zero-overhead principle is a guiding principle for the design of C++. It states that: What you don’t use, you don’t pay for (in time or space) and further: What you do use, you couldn’t hand code any better.”

To support writing such high-quality code, C++ development environments typically support a host of tools such Debugger, Profiler and Code Coverage Analyzer. And this is one of the main reasons to use C++ in performance-intensive applications.

As Close to Hardware As Possible

C++ allows developers to write low-level code that can interact with the underlying hardware and hence it is an ideal language to write device drivers and operating systems. It is also widely used to program micro-controllers. Why is this so? There are several language features that make this possible. A broad range of integral types encourages the use of appropriate types to match the underlying CPU architecture. “Bit-fields” permit structure member packing to optimize memory and to map to hardware ports. “Pointers” come in handy when directly accessing raw memory. Of course, being able to embed assembly code and efficient code generation by compilers are additional benefits!

Rich Libraries

The Standard Template Library (STL) that is part of the C++ language is quite feature rich and keeps growing with every release of the standard. In addition, for almost every task you can think of, there are commercial and/or open-source libraries.

Here is one of my favourite quotes about C++:

“C++ is biased towards programmers who think for themselves”
– Andrew Koenig [3]

Once you gain a good understanding and working proficiency in Prolog, Lisp and C++, you are ready to “take on” the world! You will be a lot wiser and learning other languages will be considerably easier. Since new languages are being designed regularly, it is essential for the working professional to become familiar with as many languages as is needed for survival. I feel it is not that important to be focused on the “Top 10 Languages” that appear periodically on the internet. They key thing is to enjoy the experience of learning and using a new language. I have always done that.

Have a great week!

References

1) Ivan Cukik, “Functional Programming in C++: How to improve your C++ programs using functional techniques”, Manning Publications, 2019.
2) David Vandevoorde, Nicolai M. Josuttis, Douglas Gregor, “C++ Templates: The Complete Guide”, Addison-Wesley, 2018.
3) Andrew Koenig, “Ruminations on C++”, Addison-wesley, 1997.

Tags:

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

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

Top