In Python, there is the aphorism from the Zen of Python (Tim Peters): "Explicit is better than implicit". This is a kind of a meta-rule in Python for writing good code. This meta-rule holds, in particular, true for the next two rules in the C++ core guidelines.

Why should you use std::array instead of C-array or gsl::array instead of C-array?

std::array knows it's length in contrast to the C-array and will not decay to a pointer as a function parameter. How easy is it to use the following function for copying arrays with the wrong length n:

voidcopy_n(const T* p, T* q, int n); // copy from [p:p+n) to [q:q+n)

Variable length arrays such as int a2[m] are a security risk, because you may execute arbitrary code or get stack exhaustion.

I sometimes hear the question in my seminars: Why should I invoke a lambda function just in place? This rule gives an answer. You can put complex initialisation in it. This in place invocation is very valuable, if you variable should become const.

If you don't want to modify your variable after the initialisation, you should make it const according to the previous rule R.25. Fine. But sometimes the initialisation of the variable consist of more steps; therefore, you can make it not const.

Have a look here. The widget x in the following example should be const after its initialisation. It cannot be const because it will be changed a few times during its initialisation.

widget x; // should be const, but:for (auto i =2; i <= N; ++i) { // this could be some
x += some_obj.do_something_with(i); // arbitrarily long code
} // needed to initialize x// from here, x should be const, but we can't say so in code in this style

Now, a lambda function comes to our rescue. Put the initialisation stuff into a lambda function, capture the environment by reference, and initialise your const variable with the in-place invoked lambda function.

Right! Don't define a (C-style) variadic function. Since C++11 we have variadic templates and since C++17 we have fold expressions. This all what we need.

You probably quite often used the (C-style) variadic function: printf. printf accepts a format string and arbitrary numbers of arguments and displays its arguments respectively. A call of printf has undefined behaviour if you don't use the correct format specifiers or the number of your arguments isn't correct.

By using variadic templates you can implement a type-safe printf function. Here is the simplified version of printf based on cppreference.com.

myPrintf can accept an arbitrary number of arguments. If arbitrary means 0, the first overload (1) is used. If arbitrary means more than 0, the second overload (2) is used. The function template (2) is quite interesting. It can accept an arbitrary number of arguments but the number must greater than 0. The first argument will be bound to value and written to std::cout (3). The rest of the arguments will be used in (4) to make a recursive call. This recursive call will create another function template myPrintf accepting one argument less. This recursion will go to zero. In this case, the function myPrintf (1) as boundary condition kicks in.

myPrintf is type-safe because all output will be handled by std::cout. This simplified implementation cannot deal with format strings such as %d, %f or 5.5f.

What's next?

There is a lot to write about expression. The C++ core guidelines has about 25 rules for them; therefore, my next post will deal with expression.

Get your e-book at leanpub:

The C++ Standard Library

Concurrency With Modern C++

Get Both as one Bundle

With C++11,C++14, and C++17 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages.

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.

I'll give you a detailed insight in the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice with more the 100 source files.

Get my books "The C++ Standard Library" (including C++17) and "Concurrency with Modern C++" in a bundle.

In sum, you get more than 550 pages full of modern C++ and more than 100 source files presenting concurrency in practice.