|Embracing Modern C++ Safely|
|Written by Harry Fairhead|
Author: Dr. John Lakos, Vittorio Romeo, Dr. Rostislav Khlebnikov and Alisdair Meredith
The idea that there are traps in every computer language isn't particularly disturbing, but to discover that you can have 1300-page book dedicated to getting it right in C++ is something of a shock.
Whenever I read Bjarne Stroustrup's books on C++ I come away thinking what a clever and clean language it is. When I then encounter some real C++, or read a book by a "C++ Wizard", I retreat to the simpler world of C as fast as I possibly can. There are more ways to do almost anything in C++ than I care to think about and now it seems many of them are unsafe.
The first chapter of the book quotes Feynman:
"If it disagrees with experiment, it's wrong. In that simple statement is the key to science".
which prompted in me:
"If a language needs 1300 pages to tell you how to use it safely then it probably isn't the language you are looking for."
But I guess if you are committed to full use of modern C++ then you probably need to read on.
The first shock is that there are only four chapters in this huge book. Not to worry, however, as each chapter is divided into small sections. Chapter 0, well yes there is an argument that all programming books should start with Chapter 0, outlines the ideas and principles behind the book. Modern C++ in this context means C++ 11 and C++ 14.
Chapter 1 is titled "Safe Features" which is a bit of a puzzle. Surely I should be told about the dangerous things I can do rather than the things that are safe? After all the safe list is open-ended and this is partly proved by the fact that it is 140 pages long. Each idea is explained and we are told why it seems safe.
Chapter 2 is titled "Conditionally Safe Features" and at about 800 pages is the bulk of the book. This, as you might expect is a list of things that are safe as long as you treat them right. Obvioously this is not a book to be read from beginning to end - even if you did what would you remember of it? So I did what most readers are going to do and dived into sections that interested me. I looked a the section on lambdas and was confronted with a very long and detailed explaination of what lambdas are and some examples of using them - this ran to over 30 pages. Finally I got to a section called Potential Pitfalls and this was about five pages pointing out where I might go wrong. As I already knew something about lambdas this was a lot of revision and at the end I didn't feel the time was well spent, but you might well disagree. The section was well written and with lots of examples - in fact perhaps to many and too long.
Chapter 3 is titled "Unsafe Features" and is about 250 pages long. This is of course the meat of the book if you are really trying to avoid things that are unsafe, but there are only nine things listed as unconditionally unsafe. That seems like not many dangerous things for a book that is so long. In this part of the book I read the section on final - something that often annoys me when I find I can't extend a system class. I read a good few pages on what final was and then on its use cases, but when I got to Potential Pitfalls I found I didn't really think that they were pitfalls. Certainly they weren't bad enough for me to consider a ban on using final - more like a set of "not good" rather then "really bad".
Overall this is a well written book and the four authors have obvious put a lot of work into producing this large volume. My reservations are more about the practical need for such a book. I rely on linting and static analysis to point out things that could be done better and I'm not sure this particular book is going to get me to write better code and if it did I don't think that the effect would last long.
If you would like to curl up with a big book of C++ examples and discussion of the how and the use cases plus some warnings about what could go wrong then this might be a book you would enjoy. For me it is of dubious value despite being well intentioned.
|Last Updated ( Tuesday, 01 March 2022 )|