Contents
If you follow some C++ social media accounts you will now and then encounter C++ quizzes and trick questions. Do we need to know the answers to be good C++ programmers?
Often, we don’t
There are C++ quizzes out there that test everyday C++ knowledge. Others come with trick questions that can only be answered correctly if you either have come across that exact same question before or know the C++ standard by heart. Distinguishing the two is important: Of course, we need to know the basics and common language rules. But we hardly ever should need to know the corner cases of the language.
In our everyday code, we should not come across corner cases of the language where we get surprising results or undefined behavior. There are a few exceptions we need to be aware of, like overflowing integral values being UB. However, there’s only a handful of those cases to remember.
On the other hand, the kind of code we often see in the trickier questions looks a bit unusual. That is simply because we come across the darkest corners of the language only when we go unusual ways. So, here’s a first answer to the question in the title: If we don’t write unusual code, we don’t need to know when or why that code might behave in surprising ways.
On the other hand, that means, of course, that we should know what we’re doing when we write code that strays from the beaten path. Beyond that, we should also make sure that anyone who has to maintain that code knows what is happening and why.
Or do we?
Social media being what it is, we see a range of reactions to those trick questions:
Interest: While those little facts often are purely academic, understanding what is happening and why can improve the overall understanding of the language. Knowing how the requirements of the standard influence each other or what the compiler does in the background for us can be beneficial in some situations. That is why hardcore C++ “nerds” often like this kind of quizzes: They improve our understanding of the underlying mechanics of the language. However, one can be a decent C++ programmer without going that deep into the language details.
“Everyone has to know this!” is something I see way too often said about things that only a handful of people need to know. The reasons can be many: Someone might not realize that C++ has a vast range of applications. A piece of special knowledge might be needed in their particular field but not in others. Some might have the very human need to feel special because they know, what only a handful of “real programmers” know.
“This is why C++ is going to die.” Well, it’s not. Yes, C++ is a complex language, and the complexity can be seen in those dark corner cases. However, most of them are there for a reason. C++ has a large set of features that make it very powerful for certain applications, and those features sometimes have unfortunate side effects. Where we don’t need those features, we may well use a different language that has a smaller and different set of corner cases. However, it is not possible to simplify C++ itself. Regardless, C++ has not died in the last 30 years and it is going to stay for a bit longer.
Conclusion
C++ quizzes and trick questions are mostly there to learn something interesting, but not necessarily useful for our daily work. It’s up to you to decide whether you spend your time with those little language details or learning a new tool or language or maybe pursuing a different hobby.