Tuesday Coding Tip 22 — Compile-time computing

●4 ●14 ●72
calendar_today • schedule1 min read
— Originally published at medium.com

Tuesday coding tips are super short posts about various tidbits mainly from C++, but also from other programming languages I use. You can also follow the #TuesdayCodingTips hashtag on Mastodon and Linkedin.


As you might know, templates in C++ are turing-complete and you can use them for compile-time computing. But they are unwieldy. So the constexpr/consteval keywords were created, so you can write a normal code and mark it as viable for compile-time execution. consteval means that the function has to be executed during compilation (and then optimized away) while constexpr gives the compiler the opportunity to do so.

#include <array>

template<unsigned limit>
consteval auto ComputeFibonacciLookup()
{
    std::array<int, limit> result = { 0 };

    unsigned n_1 = 0;
    unsigned n_2 = 1;
    for (unsigned i = 0; i < limit; i++)
    {
        result[i] = (n_1 + n_2);
        n_2 = n_1;
        n_1 = result.at(i);
    }

    return result;
}

constexpr const auto FIB_LOOKUP_TABLE = ComputeFibonacciLookup<10>();
static_assert(FIB_LOOKUP_TABLE.size() == 10u);
static_assert(FIB_LOOKUP_TABLE[0] == 1);
static_assert(FIB_LOOKUP_TABLE[1] == 1);
static_assert(FIB_LOOKUP_TABLE[2] == 2);
static_assert(FIB_LOOKUP_TABLE[3] == 3);
static_assert(FIB_LOOKUP_TABLE[4] == 5);

Why is this useful? For example, you can generate a lookup table to optimize runtime speed while maintaining the table itself via an algorithm, not hard-coded values. Or you can write compile-time unit tests for your code. These are handy the compilation will error out if you have any undefined behavior in your code (that does not happen for regular compilation).

constexpr bool divide(int a, int b)
{
    return (a / b) != 0;
}

// Static assert guaratees compile-time execution
static_assert(divide(10, 5)); // ok

// compilation error, division by zero is UB
// error: static assertion expression is not an integral constant expression
static_assert(divide(10, 0));

Extra thought: This tip was originally written three years ago. Since then, many more constexpr variants of standard containers and language constructs were added to the language. Meaning you can do more advanced stuff in the compile time. With C++26, you can even allocate memory and throw exceptions. The compiler will also check that you properly deallocate memory, which is very clever.

Edit 15.9.2026: Fixed the first code example as per Mike Dabydeen comment. Kudos for the correction!

Part 22 of 25 in Tuesday Coding Tips

2 Comments

0 votes
0
🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Tuesday Coding Tip 06 - Explicit template instantiation

Jakub Neruda - Apr 7

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

Tuesday Coding Tip 17 — Wrapping C APIs

Jakub Neruda - Jun 30

Tuesday Coding Tip 08 — Explicit template specialization

Jakub Neruda - Apr 21

Tuesday Coding Tip 05 - Object initialization in C++

Jakub Neruda - Mar 31
chevron_left
1.7k Points • 90 Badges
Brno, Czech Republic • linkedin.com/in/jakub-neruda
34Posts
28Comments
18Connections
Experienced C++ developer, team lead and hobby gamedev. I enjoy writing stuff about C++, clean code, clean architecture, and game development.

Related Jobs

Commenters (This Week)

4 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!