Title: The Modern C++ Challenge
Author: Marius Bancilla
Publisher: Packt Publishing
Year: May 2018
This week I have been going through a new book titled The Modern C++ Challenge written by Marius Bancilla. The book is a collection of programming problems (along with suggested solutions) targeting C++ . The title of the book is a bit misleading because the book does not intend to challenge your knowledge of specific C++ features, but it is a set of (mostly) general problems requiring you to code in C++. As the author himself says, it is possible to use the book as a collection of problems that one can solve in any language.
There are 100 problems in all, distributed over 12 chapters. Problems range from too simple to fairly challenging. For example the first few problems are about finding GCD, LCM, etc. It is not clear what kind of C++ challenge these pose. The author could have chosen more interesting problems instead of these. Later chapters do have interesting problems that require you to put in some effort effort, but I expected more.
Also, I think some of the problem statements are too brief and could have been explained in more detail. For instance, Problem 5 asks you to write a program for printing sexy prime pairs. Although one can google and find out the meaning of sexy prime pairs, it would have been better if it was explained as part of the problem statement itself. Interestingly, the explanation is given in the solution to this problem, but it should be the other way round.
To be fair to the author, the solutions demonstrate good use of C++. I particularly found two things interesting.
One is that the author prefers to use const qualifier even for function arguments that are passed by value, for example, you find this code on Page 14:
unsigned int gcd(unsigned int const a, unsigned int const b)
{
// Code not shown
}
I am not a great fan of this practice and I don’t use it. But there are interesting discussions about this usage on the internet and I urge you to take a look (for example, this).
The other thing I liked about the code samples (solutions) in the book is the fairly consistent use of noexcept specification as part of function declaration, wherever appropriate. noexcept has been around since C++11 and is the preferred replacement for the old throw(). The general advice is that you should use this whenever you can (and only when you can!) because it can help the compiler do interesting optimizations (see this).
I also like the fact that the author uses the C++17 library feature std::string_view() in several examples instead of the traditional std::string(). The former is an efficient way of constructing a string-like object without owning the data. Here is a good discussion on this topic. If you want the official specification, it is here.
Of course, the code is not perfect. Although I haven’t spent sufficient time to understand the finer details of all the code, or think of more efficient algorithms, I feel the author could have compiled the code with extra level of warnings enabled. For example, Problem 32 is about printing Pascal Triangle. In the solution to this problem, the function number_of_digits takes an unsigned int as argument, but at the place of call, it is passed an int argument. By looking at the code, it is clear that this will not pose any issue because the values are very much within limits. However, I would have expected that someone who takes care to declare by value arguments as const, would be careful about this too (make both arguments unsigned int or just int to be consistent). This is definitely not to take away credit for the nice book from the author, but is just a harmless observation.
If you are new to C++ and are looking for additional material to reinforce your concepts, then this book will definitely help. C++ being such a complex language, one needs all the help one can get to master the language!
Have a great weekend!
Recent Comments