It is not possible, as you realized, to use a runtime variable as a template parameter; however should you know the list of values to use at compile-time, then you can indeed have a way to invoke tests for each element of this list.
template <template <size_t> class F>
void run() {}
template <template <size_t> class F, size_t H, size_t... Tail>
void run() { F<H>()(); run<F, Tail...>(); }
Then it is just a matter of defining F:
template <size_t N>
struct BitSetPlay {
void operator()() {
std::bitset<N> b;
b.flip();
std::cout << b.to_ulong() << "\n";
}
};
Putting it altogether:
#include <bitset>
#include <iostream>
template <template <size_t> class F>
void run() {}
template <template <size_t> class F, size_t H, size_t... Tail>
void run() { F<H>()(); run<F, Tail...>(); }
template <size_t N>
struct BitSetPlay {
void operator()() {
std::bitset<N> b;
b.flip();
std::cout << b.to_ulong() << "\n";
}
};
int main() {
run<BitSetPlay, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u>();
return 0;
}
Note: this assumed a possibly discontiguous list, if it is a range you wish for then you can do without variadic templates by simply keeping track of the bounds.
std::vector<bool>dynamic_bitset.