C++ Core Guidelines: Programming at Compile Time


Today, I will continue my introduction to programming at compile time. The last post started with template metaprogramming. Here is where I pick up today and finish.

Here is the big picture, before I dive in.



 Only to remind you, the rules of the C++ Core Guidelines were my starting point:

We are just at the bottom of the triangle.

Template Metaprogramming

In the last post C++ Core Guidelines: Rules for Template Metaprogramming, I present a short template metaprogram that removed the constness from its arguments.


template<typename T>              // (2)
struct removeConst{ 
    typedef T type;               // (5)

template<typename T>              // (4)
struct removeConst<const T> { 
    typedef T type;               // (5)

int main(){
    std::is_same<int, removeConst<int>::type>::value;        // true (1)
    std::is_same<int, removeConst<const int>::type>::value;  // true (3)


What can we learn from this example?

  1. Template specialisation (partial or full) is a compile-time if.  Let me be more specific: When I use  removeConst with a non-constant int (line 1), the compiler chooses the primary or general template (line 2).  When I use a constant int (line 3), the compiler chooses the partial specialisation for const T (line 4). 
  2. The expression typedef T type serves as a return value which is in this case a type.

Now, it becomes funny.

More Meta

At runtime, we use data and functions. At compile time we use metadata and metafunctions. Quite easy, it's called meta because we do metaprogramming, but what is metadata or a metafunction? Here is the first definition.

  • Metadata: Types and integral values that are used in metafunctions.
  • Metafunction: Functions that are executed at a compile-time.

Let me elaborate more on the terms metadata and metafunction.


Metadata includes three entities:

  1. Types such as int, or double.
  2. Non-types such as integrals, enumerators, pointers, or references
  3. Templates such as std::stack

As I already mentioned, I use only types and integrals in my examples to Template Metaprogramming.


Of course, this sounds strange. Types are used in template metaprogramming to simulate functions. Base on my definition of metafunctions, constexpr functions which can be executed at compile time, are also metafunctions, but this is my topic for a later post.

Here are two types, you already know from my last post C++ Core Guidelines: Rules for Template Metaprogramming: Factorial and RemoveConst.


template <> 
struct Factorial<1>{ 
    static int const value = 1; 

template<typename T > 
struct removeConst<const T> { 
    typedef T type;  


The first metafunction returns a value and the second a type. The name value and type are just naming conventions for the return values. If a metafunction returns a value, it is called value; if it returns a type, it is called type. The type traits library to which I come in my next post follows exactly this naming convention

I think it is quite enlightening to compare functions with metafunctions.

Functions versus Metafunctions

The following function power and the meta function Power calculate pow(2, 10) at runtime and compile time.


// power.cpp

#include <iostream>

int power(int m, int n){                                // (1)
    int r = 1;
    for(int k=1; k<=n; ++k) r*= m;
    return r;                                           // (3)

template<int m, int n>                                  // (2)
struct Power{
    static int const value = m * Power<m, n-1>::value;  // (4)
template<int m>                                         // (2) 
struct Power<m, 0>{                                     // (2)  
    static int const value = 1;                         // (4)

int main(){
    std::cout << std::endl;	
    std::cout << "power(2, 10)= " << power(2, 10) << std::endl;              // (A)
    std::cout << "Power<2,10>::value= " << Power<2, 10>::value << std::endl; // (B)
    std::cout << std::endl;


Here are the first differences:

  • Arguments: The function arguments go into the round brackets ("( ... )"  in line A)) and the metafunction arguments goes into the sharp brackets ("< ...>" in line B). This also holds for the definition of the function and the metafunction. The function uses round brackets and the metafunction sharp brackets.
  • Return value: The function uses a return statement (line 3), and the metafunction the static integral constant value.

I will continue this comparison when I come to constexpr functions. Anyway, here is the output of the program.


This was easy! Right? power is executed at runtime and Power at compile time, but what is happening here?


// powerHybrid.cpp

#include <iostream>

template<int n>
int power(int m){
    return m * power<n-1>(m);

int power<1>(int m){
    return m;

int power<0>(int m){
    return 1;

int main(){
    std::cout << std::endl;
    std::cout << "power<10>(2): " << power<10>(2) << std::endl;     // (1)
    std::cout << std::endl;
    auto power2 = power<2>;                                         // (2)
    for (int i = 0; i <= 10; ++i){
        std::cout << "power2(" << i << ")= " 
                  << power2(i) << std::endl;                        // (3)
    std::cout << std::endl;


The call power<10>(2)  inline (1) uses sharp and round brackets and calculates 2 to the power of 10. This mean, 10 is the compile time and 2 the runtime argument. To say indifferently: power is a function and a metafunction. Now, I can instantiate the class template for 2 and give it the name power2 (line 2). CppInsight shows me that the compiler is instantiating power for 2. Just the function argument is not bound.


The function argument is a runtime argument and can, therefore, be used in a for loop (line 3).


What's next?

In the next post, I make a jump to the next level of the triangle and write about the type-traits library. Template metaprogramming is available in C++ since C++98, but the type-traits since C++11. You may already assume it. The type-traits library is just a cultivated form of template metaprogramming.




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, Tornike Porchxidze, 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, and Michael Young.


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



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++,



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 5538

Yesterday 8162

Week 22408

Month 140610

All 7408450

Currently are 150 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments