Speeduino
Loading...
Searching...
No Matches
Functions
static_for.hpp File Reference

A compile time unrolled loop. More...

#include <stdint.h>
#include <utility>
#include <initializer_list>

Go to the source code of this file.

Functions

template<uint8_t N, typename F >
static void static_for (F &&f)
 Generate a fixed number of calls to a given free function at compile time.
 

Detailed Description

A compile time unrolled loop.

GCC is surprisingly conservative at unrolling loops with -funroll-loops (and even -funroll-all-loops):

In summary, it's designed for very simple cases where loop overhead is obviously high

See also
https://godbolt.org/z/6KGWajPaY

Function Documentation

◆ static_for()

template<uint8_t N, typename F >
static void static_for ( F &&  f)
inlinestatic

Generate a fixed number of calls to a given free function at compile time.

Usage:

static inline void foo(uint8_t index, time_t *currTime, const char *message) {
// Do something
}
time_t currTime = time(NULL);
char message[] = "Unrolled!";
// Equivalent of
// foo(3, &currTime, message);
// foo(4, &currTime, message);
// foo(5, &currTime, message);
// foo(6, &currTime, message);
static_for<3, 7>::repeat_n(foo, &currTime, message);
Template Parameters
NThe upper bound (exclusive)
FCallable type
Parameters
fCallable instance