mercredi 17 février 2021

Recursion-free expansion of variadic template into array access

Consider the follow non-type variadic template function:

template <typename dummy = void>
void write_at_offsets(volatile char *p) {
}

template <size_t OFF, size_t... OFFs>
void write_at_offsets(volatile char *p) {
    p[OFF] = 1;
    write_at_offsets<OFFs...>(p);
}

It uses the recursive approach to write a 1 at the offsets specified in the template parameter pack.

Can this be written succinctly without using recursion in C++11, e.g., by expanding the whole pack in one shot?

Aucun commentaire:

Enregistrer un commentaire