Unleashing C++ Power: Richness in Low-Level Code

How many times do you read a reflexion  concerning C++ like this one:

C++ is a difficult language even for experienced C++ developers. Even for the simplest algorithms you have to explain many of the language subtleties. Consider a Hello World example:

#include <iostream>

int main()
{
  std::cout << "Hello World!" << std::endl;
}

What’s that #include command? What’s std::cout. Why the ::? What is <<? Ohhh, it is an overloaded operator! What’s an overloaded operator? Sooo, for ints, it does bit shifting, but for whatever std::cout is, it outputs stuff to the console. Ohhh, std::cout is a stream, and streams have their << and >> operator overloaded.

Let’s see the same sample in Python:

print("Hello World!")

 

Maybe this reflexion is true if we use directly what the standard provides as methods and utilities. But it’s not the case for real C++ projects. Indeed in many C++ projects, we have a code like this:

#include "utilities"

int main()
{
  print("Hello World!");
}

And the fact that we have a low-level mechanism for the console by using std::cout and all the possibilities provided give to the language a richness not existing in the other languages. It’s up to you to make the most of its richness to provides an easy to read, maintain and efficient code.

The same remark could be applied to any C++ code considered as overcomplicated. In C++ we have all that you need to simplify its use and provide a library which hides the complexity and use it like an easy language.

Let’s take another example of accessing a file and read all its content. With C# you can do it like this

string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");

But is there any limitation to do the same in C++

string text = FileHelper.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");

However many developers could ask: Why I need to another library to do what I want easily, why to not embed these facilities in the standard library?

The better answer is maybe another question: Why there are a ton of libraries for each language? sooner or later we need to use external libraries even for the easiest language.  And in the real projects even for python, many times in house libraries are used. And of course, a ton of external libraries are also used.

Don’t forget that a language programming is destined to engineers who have some basic skills to know how to use the possibilities provided. In any domain, isn’t the work of the engineer to analyze, adapt and give an efficient solution to a specific problem?  C++ is maybe low level but give us more possibilities, its richness is needed to resolve the problems efficiently.

Personally, in many projects, I worked on in C++. We already have an in house library to simplify all the recurrent needs, each developer having a recurrent need suggest to add a utility to the library. And all the developers benefit from these facilities. This approach does not need to have C++ gurus or experts in our team.  This is simply common sense.