(1) avoids best practice (RAII) using...
(2) a flawed rationale (ignoring the existence of compiler optimizations)...
(3) without measurement to demonstrate the supposed flaw in the best practice (which would have surfaced the flawed rationale)...
(4) and therefore implements a micro-optimization with no actual win...
(5) and introduces a bug in the process (it's not exception safe).
- these are the days of C++11 where scoped locks are readily available
- premature optimization
- macros are evil
Personally I never liked straight C.
For me it was just a short transition between Turbo Pascal and C++, only to be used when required to do so. Around 1993 or so.
I devoured every book and publication about C++ I could put my hands on. Always trying to educate others how to write safe and portable C++.
Nowadays I am into JVM and .NET mostly, but when I reach for C++ it is with regard to the latest standards.
Every now and then, I still see C++ code that is basically the C subset without using any improvements of C++ over C.
No exceptions, no boost, no lambda, no rtti, streams only for logging, avoid operator overloading, etc.
> no boost
Not true, select parts of Boost are available.
> no lambda
The C++11 features are being rolled in gradually. Lambda expressions are now permitted.
> no rtti
The guide says "avoid RTTI" and urges the developer to come up with a different design. This is very much in line with C++ best practice overall. There's no ban on RTTI.
Do not use lambda expressions, or the related std::function or std::bind utilities.
As a general rule, if your C++ compiler can statically see the layout of your structures and the implementation of your functions, it will produce code as efficient as inlining everything yourself. Sufficiently complex functions make the benefits of inlining a judgment call on the compiler's part, but for something as simple as an RAII scoped lock, the abstraction is free.
> The only gotcha is, there is some overhead involved in this approach. The class instance takes up some space on the stack (several bytes) for every lock acquisition.
Not so fast. A decent compiler will eliminate the overhead and not actually allocate stack space for the lock. Here's an example using one of the lock guards in C++:
GCC 4.8.1 generates identical code for process1 and process2.