C++ Core Guidelines: More Rules for Declarations

In this post, I will finish the rules for declarations. The remaining rules for declarations are not especially sophisticated but important for high code quality.



Let's start. Here is the first overview before we dive into the details.

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.

ES.25: Declare an object const or constexpr unless you want to modify its value later on

Why should you use const or constexpr for your variable declaration if possible? I have a lot of good reasons:

  • You express your intention.
  • The variable cannot be changed by accident.
  • const or constexpr variables are by definition thread-safe.
    • const: You have to guarantee that the variable is initialised in a thread-safe way.
    • constexpr: The C++ runtime guarantees, that the variable is initialised in a thread-safe way.

ES.26: Don’t use a variable for two unrelated purposes

Do you like such kind of code?

void use()
    int i;
    for (i = 0; i < 20; ++i) { /* ... */ }
    for (i = 0; i < 200; ++i) { /* ... */ } // bad: i recycled


I hope not. Put the declaration of i into the for loop and you are fine. i will be bound to the lifetime of the for a loop. 

void use()
    for (int i = 0; i < 20; ++i) { /* ... */ }
    for (int i = 0; i < 200; ++i) { /* ... */ } 


With C++17, you can declare your i just in an if or switch statement: C++17 - What's new in the language?

ES.27: Use std::array or stack_array for arrays on the stack

10 years ago, I thought that creating a variable-length array on the stack is ISO C++. 


const int n = 7;
int m = 9;

void f()
    int a1[n];
    int a2[m];   // error: not ISO C++
    // ...



In the first case, you should use an std::array and in the second case, you can use a gsl::stack_array from the Guideline support library (GSL)

const int n = 7;
int m = 9;

void f()
    std::array<int, n> b1;
    gsl::stack_array<int> b2(m);
    // ...


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

std::array knows its 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: 


void copy_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. 

ES.28: Use lambdas for complex initialization, especially of const variables

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 your 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.


const widget x = [&]{
    widget val;                                // widget has a default constructor
    for (auto i = 2; i <= N; ++i) {            // this could be some
        val += some_obj.do_something_with(i);  // arbitrarily long code
    }                                          // needed to initialize x
    return val;


Admittedly, it looks a little bit strange to invoke a lambda function just in place, but from the conceptional view, I like it. You put the whole initialisation stuff just in a function body. 

ES.30ES.31,  ES.32 and ES.33

I will only paraphrase the next four rule to macros. Don't use macros for program test manipulation or for constants and functions. If you have to use them use unique names with ALL_CAPS.

ES.34: Don’t define a (C-style) variadic 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 that 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 print 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.cpp

#include <iostream>
void myPrintf(const char* format){                         // (1)
    std::cout << format;
template<typename T, typename... Targs>                    // (2)
void myPrintf(const char* format, T value, Targs... Fargs) 
    for ( ; *format != '\0'; format++ ) {
        if ( *format == '%' ) {
           std::cout << value;                             // (3)
           myPrintf(format+1, Fargs...);                   // (4)
        std::cout << *format;
int main(){
    myPrintf("% world% %\n","Hello",'!',123);            // Hello world! 123


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 have about 25 rules for them; therefore, my next post will deal with expression.




Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, Alessandro Pezzato, Evangelos Denaxas, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Dimitrov Tsvetomir, Leo Goodstadt, Eduardo Velasquez, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Haken Gedek, and Bernd Mühlhaus.


Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, and Rusty Fleming.



My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small


My special thanks to PVS-Studio PVC Logo



I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)


Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.


Contact Me

Modernes C++,



0 #1 Forex Robots 2018-04-07 06:37
Very good write-up. I certainly appreciate this site. Continue the good work!

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Interactive Course: The All-in-One Guide to C++20

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code


Today 140

Yesterday 7842

Week 7982

Month 41360

All 8160506

Currently are 212 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments